1225. [S/W 문제해결 기본] 7일차 - 암호생성기
8개의 수를 입력받아서 숫자 중 하나가 0보다 같거나 작아질 때 까지 사이클을 반복한다. 이때 마지막으로 남은 8자리의 숫자가 암호가 된다.
Queue
를 사용한다.
1~5까지 감소를 반복하므로, 감소할 크기를 변수 i로 지정하고, i>5
가 되면 i=1
으로 초기화!
현재 front를 poll()해서 temp
라는 변수에 담고, i
만큼 감소시킨 뒤 다시 큐에 넣어주는 과정을 반복한다. 만약 하나라도 temp가 0보다 같거나 작아지면 반복문을 멈추고 queue의 데이터를 출력한다.
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.io.FileInputStream;
class Solution
{
public static void main(String args[]) throws Exception
{
Scanner sc = new Scanner(System.in);
int T = 10;
for(int test_case = 1; test_case <= T; test_case++)
{
int t = sc.nextInt();
Queue<Integer> queue = new LinkedList<>();
int temp;
for (int i=0; i<8; i++) {
temp = sc.nextInt();
queue.offer(temp);
}
int i=1;
while(true) {
temp = queue.poll();
temp -= i;
if (temp <= 0) break;
queue.offer(temp);
i++;
if (i>5) i=1;
}
queue.offer(0);
System.out.printf("#%d", test_case);
for (i=0; i<8; i++) {
System.out.printf(" %d", queue.poll());
}
System.out.println();
}
}
}