구현(Implementation)
백준 1871번: 좋은 자동차 번호판
cepiloth
2018. 7. 19. 17:30
반응형
https://www.acmicpc.net/problem/1871
1. 문제
앨버타의 자동차 번호판은 ABC-0123 (세 글자, 네 숫자)와 같이 두 부분으로 나누어져 있다.
좋은 번호판은 첫 번째 부분의 가치와 두 번째 부분의 가치의 차이가 100을 넘지 않는 번호판이다.
글자로 이루어진 첫 번째 부분의 가치는 글자를 26진법 숫자처럼 계산하다. (각 자리가 [A..Z]) 예를 들어, "ABC"의 가치는 28 (0*26^2 + 1*26^1 + 2*26^0)이 된다. "ABC-0123"은 |28 - 123| <= 100 이기 때문에, 좋은 번호판이다.
자동차 번호판이 주어졌을 때, 좋은 번호판인지 아닌지를 구하는 프로그램을 작성하시오.
2. 알고리즘
키워드 - 문자열, 파싱, 수학
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 | #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> #include <math.h> using namespace std; #define MAX_SIZE 100 #define INF 0x7fffffff #define CENDL "\n" #define ll long long int transfrom_digit_to_26(int index, int d) { return d * pow(26, index); } int transfrom_digit(string s) { int a = transfrom_digit_to_26(2,s[0] - 'A'); int b = transfrom_digit_to_26(1,s[1] - 'A'); int c = transfrom_digit_to_26(0,s[2] - 'A'); return a + b + c; } int main() { cin.tie(0); std::ios::sync_with_stdio(false); int n; cin >> n; while(n--) { string s; cin >> s; string alphabet = s.substr(0,3); int alpha_int = transfrom_digit(alphabet); string number = s.substr(4, s.size()); int number_int = atoi(number.c_str()); if (abs(alpha_int - number_int) <= 100) { cout << "nice" << CENDL; } else { cout << "not nice" << CENDL; } } return 0; } | cs |
반응형