Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
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
Tags
more
Archives
Today
Total
관리 메뉴

혼종 꼬지마루

[백준] 15874 - Caesar Cipher 본문

Algorithms/SIMULATION

[백준] 15874 - Caesar Cipher

꼬지마루 2018. 9. 17. 20:09

간단한 시뮬레이션 문제


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