PS/Java

[SWEA] 1228. [S/W 문제해결 기본] 8일차 - 암호문1

siyamaki 2021. 3. 10. 16:51

0 ~ 999999 사이의 수를 나열하여 만든 암호문이 있다.

암호문을 급히 수정해야 할 일이 발생했는데, 이 암호문은 특수 제작된 처리기로만 수정이 가능하다.

이 처리기는 다음과 같이 1개의 기능을 제공한다.

1. I(삽입) x, y, s : 앞에서부터 x의 위치 바로 다음에 y개의 숫자를 삽입한다. s는 덧붙일 숫자들이다.[ ex) I 3 2 123152 487651 ]

위의 규칙에 맞게 작성된 명령어를 나열하여 만든 문자열이 주어졌을 때, 암호문을 수정하고, 수정된 결과의 처음 10개 숫자를 출력하는 프로그램을 작성하여라.


import java.util.ArrayList;
import java.util.Scanner;

public class 암호문1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        for(int tc = 1; tc <= 10; tc++) {
            int n = scanner.nextInt();

            ArrayList<Integer> arr = new ArrayList<>();

            for(int i = 0; i < n; i++) {    // 암호문의 길이
                int t = scanner.nextInt();
                arr.add(t);
            }

            int m = scanner.nextInt(); // 명령어의 개수

            for(int i = 0; i < m; i++) {
                scanner.next();     // l 문자를 건너뛰기 위함
                int x = scanner.nextInt();  // x의 위치에
                int y = scanner.nextInt();  // 다음 y개 숫자 삽입.

                for(int j = 0; j < y; j++) {
                    int s = scanner.nextInt();
                    arr.add(x + j, s);
                }
            }

            System.out.print("#" + tc + " ");
            for(int i = 0; i < 10; i++) {
                System.out.print(arr.get(i) + " ");
            }
            System.out.println();
        }

    }
}