본문 바로가기

프로그래머스/lv0

암호 해독

/* 암호 해독
 * 암호화된 문자열 cipher를 주고받습니다.
 * 그 문자열에서 code의 배수 번째 글자만 진짜 암호입니다.
 * 문자열 cipher와 정수 code가 매개변수로 주어질 때 해독된 암호 문자열을 return
 *
 * cipher                       code    result
 * "dfjardstddetckdaccccdegk"   4       "attack"
 * "pfqallllabwaoclk"           2       "fallback"
 *
 * "dfjardstddetckdaccccdegk" 의 4번째, 8번째, 12번째, 16번째, 20번째, 24번째 글자를 합친 "attack"을 return합니다.
 * "pfqallllabwaoclk" 의 2번째, 4번째, 6번째, 8번째, 10번째, 12번째, 14번째, 16번째 글자를 합친 "fallback"을 return합니다.
 */

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class programmer_0_25 {
    static String a1 = "dfjardstddetckdaccccdegk";  static int n1 = 4;
    static String a2 = "pfqallllabwaoclk";          static int n2 = 2;
 
    public String solution(String cipher, int code) {
        String answer = "";
        System.out.println(cipher.length());
 
        String[] ar = cipher.split("");
        for(int i = code; i <= ar.length; i = i + code){
            answer += ar[i-1];
        }
        return answer;
    }
    public static void main(String args[]){
        programmer_0_25 t = new programmer_0_25();
        System.out.println("---------------------------------------");
        System.out.println("result = " + t.solution(a1,n1));
        System.out.println("---------------------------------------");
        // System.out.println("result2 = " + t.solution(a2,n2));
        // System.out.println("---------------------------------------");
    }
}
cs

'프로그래머스 > lv0' 카테고리의 다른 글

영어가 싫어요  (0) 2022.12.05
대문자와 소문자  (0) 2022.12.05
369게임  (0) 2022.12.05
가까운 수  (1) 2022.12.05
삼각형의 완성조건 (1)  (0) 2022.12.05