Lined Notebook

[1225] [Java] 암호생성기

by HeshAlgo

<암호생성기>

문제 설명

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14uWl6AF0CFAYD&categoryId=AV14uWl6AF0CFAYD&categoryType=CODE

 

내 생각

단순히 큐를 이용해 문제를 풀었습니다. 빼는 숫자를 8까지 빼는 줄 알고 문제를 풀다가 시간이 오래 걸렸습니다. 5까지 뺐을때 1사이클로 간주되고 그 이후에는 다시 1로 초기화해서 빼야합니다.

 

푼 시간

33분 33초

 

작성 코드

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
// 암호생성기
import java.io.FileInputStream;
import java.util.*;
 
public class E1225 {
 
    public static void main(String args[]) throws Exception {
        String path = "input.txt";
        System.setIn(new FileInputStream(path));
        Scanner sc = new Scanner(System.in);
        
        for(int test_case = 1; test_case <= 10; test_case++) {
            Queue<Integer> q = new LinkedList<Integer>();
            int t = sc.nextInt();
            
            for (int i = 0; i < 8; i++) {
                int num = sc.nextInt();
                q.add(num);
            }
            
            int cnt = 1;
            
            while (true) {    
                int num = q.poll();
                num -= cnt; 
            
                if (num <= 0) {
                    num = 0;
                    q.add(num);
                    break;
                }
    
                q.add(num);
                if (cnt == 5)
                    cnt = 0;
                cnt++;
            }
            
            System.out.print("#" + test_case + " ");
            for (int i = 0; i < 8; i++) {
                System.out.print(q.poll() + " ");
            }
            System.out.println();
            
        }
        
    }
 
    
}
 
 

 

실행 결과

블로그의 정보

꾸준히 공부하는 개발 노트

HeshAlgo

활동하기