[SWEA] 암호문3

문제 보러가기

제한사항

이외의 제한사항은 없다.

첫번째 생각

명령어 I는 arrayList의 인덱스를 지정하여 위치를 정해줄 수 있는 add 함수를 사용하여 해결한다.

1
aList.add(x+(index++), sc.nextInt()

명령어 D는 arrayList의 remove를 사용하여 해결한다.

1
aList.remove(x);

명령어 A는 arrayList의 add 함수 중 인덱스가 없는 함수를 사용하여 해결한다.

1
aList.add(sc.nextInt());

JAVA Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.ArrayList;
import java.util.Scanner;

public class Solution {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		for(int tc=1; tc<=10; tc++) {
			ArrayList<Integer> aList = new ArrayList<>();
			int N = sc.nextInt();
			for(int i=0; i<N; i++) {
				aList.add(sc.nextInt());
			}
			
			int M = sc.nextInt(); // 명령어의 갯수
			for(int i=0; i<M; i++) {
				int index = 0;
				String c = sc.next();
				switch (c) {
					case "I": {
						int x = sc.nextInt();
						int y = sc.nextInt();
						for(int j=0; j<y; j++) { 
							aList.add(x+(index++), sc.nextInt());
						}
						break;
					}
					case "D": {
						int x = sc.nextInt();
						int y = sc.nextInt();
						for(int j=0; j<y; j++) {
							aList.remove(x);
						}
						break;
					}
					case "A": {
						int y= sc.nextInt();
						for(int j=0; j<y; j++) {
							aList.add(sc.nextInt());
						}
						break;
					}
				}
			}
			
			System.out.print("#"+tc+" ");
			for(int i=0; i<10; i++) {
				System.out.print(aList.get(i)+" ");
			}
			System.out.println();
		}
	}
}