[SWEA] 1225. 암호생성기

new Dean( );·2021년 8월 2일
0

알고리즘

목록 보기
9/30

문제

1225. [S/W 문제해결 기본] 7일차 - 암호생성기
8개의 수를 입력받아서 숫자 중 하나가 0보다 같거나 작아질 때 까지 사이클을 반복한다. 이때 마지막으로 남은 8자리의 숫자가 암호가 된다.

  • 1사이클 : front의 값에서 1을 감소한 뒤 맨 뒤로 보냄 -> 2를 감소하고 맨 뒤로 보냄 -> ... -> 5를 감소하고 맨 뒤로 보냄 -> 1사이클 종료

풀이

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();
		}
	}
}

0개의 댓글