혼종 꼬지마루
[백준] 15874 - Caesar Cipher 본문
간단한 시뮬레이션 문제
O(S)면 끝나기 때문
입력받은 K 를 26으로 나눈 나머지로 바꿔 준다.
그리고 한 문자식 꺼내어 보며 k씩 올려준다.
이때 주의할 것은 소문자의 경우 k를 올려주면 아스키 코드값을 넘어가버린다.
중간에 한번 변환시키고 연산하면 바로 나옴
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 | #include <iostream> #include <string> #include <cstdio> #define MX 100002 using namespace std; string str; int k, s; int main(void) { scanf("%d %d\n", &k, &s); getline(cin, str); k %= 26; for (int i = 0; i < s; i++) { if ('A' <= str[i] && str[i] <= 'Z') { int tmp = str[i] - 'A'; tmp += k; if (tmp >= 26) tmp -= 26; str[i] = tmp + 'A'; } if ('a' <= str[i] && str[i] <= 'z') { int tmp = str[i] - 'a'; tmp += k; if (tmp >= 26) tmp -= 26; str[i] = tmp + 'a'; } cout << str[i]; } cout << "\n"; return 0; } | cs |
'Algorithms > SIMULATION' 카테고리의 다른 글
[프로그래머스] (2018)KAKAO BLIND - 오픈채팅방 (0) | 2018.11.09 |
---|---|
[백준] 16235 - 나무 재태크 (0) | 2018.11.08 |
[SWEA] 5650 - 핀볼 게임 (0) | 2018.09.21 |
[SWEA] 5658 - 보물상자 비밀번호 (0) | 2018.09.21 |
[SWEA] 5656 - 벽돌 깨기 (4) | 2018.09.21 |