[SWEA] 암호생성기

문제 보러가기

제한사항

이외의 제한사항은 없다.

첫번째 생각

첫번째 숫자를 1 감소한 뒤, 맨 뒤로 보낸다 => Queue의 형태와 같다.

queue의 offer와 poll을 활용하여 문제를 해결한다.

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
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Solution {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		for(int tc=1; tc<=10; tc++) {
			sc.nextInt();
			Queue<Integer> q = new LinkedList<Integer>();
			for(int i=0; i<8; i++) {
				q.offer(sc.nextInt());
			}
			boolean check = true;
			while ( check ) {
				for(int i=1; i<=5; i++) {
					int tmp = q.poll()-i;
					
					if ( tmp <= 0 ) {
						q.offer(0);
						check = false;
						break;
					}
					q.offer(tmp);
				}
			}
			System.out.print("#"+tc+" ");
			for(int i=0; i<8; i++) {
				System.out.print(q.remove()+" ");
			}
			System.out.println();
		}
		
	}

}