Skip to content

Instantly share code, notes, and snippets.

@mingyu-lee
Created August 23, 2018 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mingyu-lee/42fdf6192320c8bec513ca77e8a804d4 to your computer and use it in GitHub Desktop.
Save mingyu-lee/42fdf6192320c8bec513ca77e8a804d4 to your computer and use it in GitHub Desktop.
BOJ_11655_ROT13_개선
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* ROT13 카이사르 암호
*
* Problem
* ROT13은 카이사르 암호의 일종으로 영어 알파벳을 13글자씩 밀어서 만든다.
*
* 예를 들어, "Baekjoon Online Judge"를 ROT13으로 암호화하면 "Onrxwbba Bayvar Whqtr"가 된다. ROT13으로 암호화한 내용을 원래 내용으로 바꾸려면 암호화한 문자열을 다시 ROT13하면 된다. 앞에서 암호화한 문자열 "Onrxwbba Bayvar Whqtr"에 다시 ROT13을 적용하면 "Baekjoon Online Judge"가 된다.
*
* ROT13은 알파벳 대문자와 소문자에만 적용할 수 있다. 알파벳이 아닌 글자는 원래 글자 그대로 남아 있어야 한다. 예를 들어, "One is 1"을 ROT13으로 암호화하면 "Bar vf 1"이 된다.
*
* 문자열이 주어졌을 때, "ROT13"으로 암호화한 다음 출력하는 프로그램을 작성하시오.
*
* Input
* 첫째 줄에 알파벳 대문자, 소문자, 공백, 숫자로만 이루어진 문자열 S가 주어진다. S의 길이는 100을 넘지 않는다.
*
* Output
* 첫째 줄에 S를 ROT13으로 암호화한 내용을 출력한다.
*
* https://www.acmicpc.net/problem/116
*/
public class ROT13_2 {
public static void main(String[] args) throws IOException {
for (char tempChar : new BufferedReader(new InputStreamReader(System.in)).readLine().toCharArray()) {
if ('A' <= tempChar && tempChar <= 'Z') {
System.out.printf("%c", (char)((tempChar - 'A' + 13) % 26 + 'A'));
} else if ('a' <= tempChar && tempChar <= 'z') {
System.out.printf("%c", (char)((tempChar - 'a' + 13) % 26 + 'a'));
} else {
System.out.printf("%c", tempChar);
}
}
}
}
@mingyu-lee
Copy link
Author

개선된 코드, 알파벳이 26개인 것을 고려하여 +13이 z또는 Z가 넘어가면 나머지만큼 a 또는 A에서 더해줌

@mingyu-lee
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment