[Java] 백준11655번 ROT13

2022. 4. 7. 22:38
728x90

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

 

11655번: ROT13

첫째 줄에 알파벳 대문자, 소문자, 공백, 숫자로만 이루어진 문자열 S가 주어진다. S의 길이는 100을 넘지 않는다.

www.acmicpc.net

이번 문제는 13씩 더하고 알파벳 숫자를 벗어나면 처음부터 다시 남은 수를 더해주면 풀리는 문제입니다.

 

 

package BJ;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class BJ_ROT13 {
	static String str;
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		
		str = br.readLine();
		
		for (int i = 0; i < str.length(); i++) {
			int a = str.charAt(i);
			
			if(a >= 65 && a <= 90) {
				a += 13;
				
				if(a > 90) {
					int b = a - 91;
					a = 65+b;
				}
			} else if (a >= 97 && a <= 122) {
				a += 13;
				
				if(a > 122) {
					int b = a-123;
					a = 97 + b;
				}
			}
			sb.append((char)a);
		}
		
		System.out.println(sb.toString());
		
	}

}

 

Ysik Github : https://github.com/Y1sik/Algorithm/blob/main/BJ/BJ_ROT13.java

반응형

BELATED ARTICLES

more