๐Ÿ“ Coding Test Study/Algorithm Problem

[C++][Baekjoon][๋ฌธ์ž์—ด] 11655๋ฒˆ ROT13

ibelieveinme 2024. 2. 17. 20:40
728x90

* ๋ฌธ์ œ

 

11655๋ฒˆ: ROT13

์ฒซ์งธ ์ค„์— ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž, ์†Œ๋ฌธ์ž, ๊ณต๋ฐฑ, ์ˆซ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๋ฌธ์ž์—ด S๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. S์˜ ๊ธธ์ด๋Š” 100์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

www.acmicpc.net

 

 

์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์„ ROT13 ์œผ๋กœ ์•”ํ˜ธํ™”ํ•œ ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

 

* ๋ฌธ์ œํ’€์ด

ROT13์€ ์•ŒํŒŒ๋ฒณ์„ 13๊ธ€์ž์”ฉ ๋ฐ€์–ด์„œ ๋งŒ๋“œ๋ฏ€๋กœ ASCII ์ฝ”๋“œ๋กœ ํ‘œํ˜„๋œ ์•ŒํŒŒ๋ฒณ ์ˆซ์ž์— 13์„ ๋”ํ•ด์„œ ๋‹ค์‹œ ๋ฌธ์ž์—ด๋กœ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

์ด ๋•Œ, z(Z) ๊ฐ’์ด ๋„˜์–ด๊ฐˆ ๋•Œ๋Š” ๋‹ค์‹œ a(A) ๊ฐ’๋ถ€ํ„ฐ ์นด์šดํŠธํ•ด์ค˜์•ผ ํ•œ๋‹ค. ROT13์€ ์•ŒํŒŒ๋ฒณ ๊ฐœ์ˆ˜์ธ 26์˜ ์ •ํ™•ํ•œ ๋ฐ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹จ์ˆœํžˆ ์•ŒํŒŒ๋ฒณ + 13 ๊ฐ’์—์„œ 26์„ ๋นผ์ฃผ๋ฉด ๋œ๋‹ค.

 

 

* ์•Œ์•„๋‘˜ ๊ฒƒ

1) ASCII ์ฝ”๋“œ

https://www.researchgate.net/figure/ASCII-table-for-the-alphabet_fig1_338623532

 

2) int to string

to_string(97);

 

 

3) int to char

์•”์‹œ์ , ๋ช…์‹œ์  ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•จ.

์•”์‹œ์  ๋ณ€ํ™˜์€ ๊ทธ๋ƒฅ char ch = 97; ํ•˜๋ฉด ๋˜๊ณ 

๋ช…์‹œ์  ๋ณ€ํ™˜์€ (char)97; ์ด๋Ÿฌ๋ฉด ๋œ๋‹ค.

 

4) char to int

int a = (int)'a';

 

 

* ์ฝ”๋“œ

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

int main() {

	const int SCREAT_NUM = 13;
	const int ALPHABET_NUM = 26;

	const int a = (int)'a';
	const int z = (int)'z';
	const int A = (int)'A';
	const int Z = (int)'Z';

	string str = "";
	
	getline(cin, str);
	for (int i = 0; i < str.size(); i++) {
		//์†Œ๋ฌธ์ž
		if (str[i] >= a && str[i] <= z) {
			if (str[i] + SCREAT_NUM > z) {
				str[i] = (str[i] + SCREAT_NUM) - ALPHABET_NUM;
			}
			else {
				str[i] += SCREAT_NUM;
			}
		}
		//๋Œ€๋ฌธ์ž
		if(str[i] >= A && str[i] <= Z){
			if (str[i] + SCREAT_NUM > Z) {
				str[i] = (str[i] + SCREAT_NUM) - ALPHABET_NUM;
			}
			else {
				str[i] += SCREAT_NUM;
			}
		}
	}

	cout << str << "\n";
}

 

 

728x90