[BaekJoon] 5598번 - 카이사르 암호 [Java][C++]
[BaekJoon] 5598번 - 카이사르 암호 [Java][C++]
1. 문제 풀이
주어진 카이사르 단어로부터 원래 단어를 찾아 출력하는 문제로 카이사르 단어는 주어진 단어의 각 알파벳을 오른쪽으로 3칸 민 형태이다. 따라서 주어진 카이사르 단어의 각 알파벳을 왼쪽으로 3칸 밀면 원래 단어를 찾을 수 있다. 이때 $Z$ 와 $A$ 가 원형으로 이어져 있으므로 유니코드 상에서 인덱스 처리에 주의해야 한다.
2. 코드
1. 풀이 [Java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
String str = br.readLine();
for (char c : str.toCharArray()) {
if (c <= 'C') {
sb.append((char) (c - 3 + 26));
} else {
sb.append((char) (c - 3));
}
}
System.out.println(sb);
}
}
2. 풀이 [C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
for (char c : s) {
if (c <= 'C') {
cout << (char)(c - 3 + 26);
} else {
cout << (char)(c - 3);
}
}
}
This post is licensed under CC BY 4.0 by the author.