정수론(Number theory)
백준 2153번: 소수 단어
cepiloth
2018. 7. 16. 21:42
반응형
https://www.acmicpc.net/problem/2153
1. 문제
입력 받은 문자열을 숫자로 치환하여 총합에 값이 소수인지 판단하는 문제
2. 알고리즘
키워드 - 정수론, 수학
접근법 - 문자의 총길이는 20 임으로 문자열으로 입력 받아서 총합을 구하여 처리 한다.
3. 코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #include <iostream> #include <sstream> #include <string> #include <algorithm> #include <functional> #include <vector> #include <list> #include <queue> #include <deque> #include <map> #include <set> #include <stack> #include <cstring> using namespace std; #define MAX_SIZE 100 #define INF 0x7fffffff #define CENDL "\n" #define ll long long int prime(int n){ // 문제 제약 조건 1 도 소수로 치자고 했음-_- 여기서 혼수상태 if (n == 1) { return 1; } if (n == 2) { return 1; } for(int i=2;i<n;i++) { if(n % i == 0) { return 0; } } return 1; } int main() { cin.tie(0); std::ios::sync_with_stdio(false); string s; cin >> s; const int size = s.size(); int sol = 0; for (int i=0; i<size; i++) { sol += tolower(s[i]) - 'a' + 1; } if (prime(sol)) { cout << "It is a prime word." << CENDL; } else { cout << "It is not a prime word." << CENDL; } return 0; } | cs |
반응형