๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/PS

[๋ฐฑ์ค€] ROT13(11655), C++

by candosh 2024. 11. 12.

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

https://www.acmicpc.net/problem/11655

 

โœ๐Ÿป ๋ฌธ์ œ ์„ค๋ช…

๐Ÿ’๐Ÿป‍โ™€๏ธ ๋‚ด ํ’€์ด

๊ณต๋ฐฑ์ด ์žˆ๋Š” ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ ๋ฐ›์•„์•ผ ํ•˜๋‹ˆ getline()ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

[ getlineํ•จ์ˆ˜ ์‚ฌ์šฉ ์˜ˆ์‹œ ]

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s;
    getline(cin, s);
    cout << s << "\n";
}

 

๊ทธ๋ฆฌ๊ณ  ROT13์€ ๊ธฐ๋ณธ ๋ฌธ์ž์—์„œ +13์„ ํ•œ ๊ฐ’์œผ๋กœ ์•”ํ˜ธํ™”ํ•ด์ฃผ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ, ์ž…๋ ฅ ๋ฐ›์€ ๋ฌธ์ž์—ด๋“ค์„ +13์œผ๋กœ ์•”ํ˜ธํ™”ํ•ด์„œ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

๋งˆ์ง€๋ง‰์œผ๋กœ ์ฃผ์˜ํ•  ์ ์€ ๋Œ€๋ฌธ์ž์™€ ์†Œ๋ฌธ์ž๋ฅผ ๋ถ„๋ฅ˜ํ•˜์—ฌ ์•”ํ˜ธํ™”ํ•ด์ค˜์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ๊ณผ +13 ํ–ˆ์„ ๋•Œ ํ•ด๋‹น ๋Œ€๋ฌธ์ž, ์†Œ๋ฌธ์ž ์•„์Šคํ‚ค์ฝ”๋“œ ๋ฒ”์œ„๋ฅผ ๋„˜์–ด์„œ๋ฉด -26์„ ํ•ด์ค˜์„œ ๋‹ค์‹œ ๋ฒ”์œ„ ์•ˆ์—์„œ ์•”ํ˜ธํ™”๋  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ค˜์•ผ ํ•œ๋‹ค. (A-65๋ถ€ํ„ฐ, a-97๋ถ€ํ„ฐ)

 

๐Ÿค– ์ฝ”๋“œ

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string str;
    getline(cin, str);

    for (int i = 0; i < str.size(); i++)
    {
        if (str[i] >= 65 && str[i] < 97)
        {
            if (str[i] + 13 > 90)
            {
                str[i] -= 13; // str[i] + 13 -26
            }
            else
                str[i] += 13;
        }
        else if (str[i] >= 97 && str[i] < 123)
        {
            if (str[i] + 13 > 122)
            {
                str[i] -= 13;
            }
            else
                str[i] += 13;
        }
    }

    cout << str << "\n";

    return 0;
}