프로그래밍 언어/JAVA

백준 > 11866 요세푸스 문제 0

B612 2022. 2. 22. 14:30
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
import java.util.Scanner;
import java.util.LinkedList;
import java.util.Queue;
 
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int k = scan.nextInt();
        int j = 1;
        
        Queue<Integer> que = new LinkedList<Integer>();
 
        for (int i=1;i<n+1;i++) {
            que.add(i);
        }
        
        System.out.print("<");
        
        while(que.size() > 1) {
            for (int q = 1; q < k; q++) {
                que.add(que.poll());
            }
            System.out.print(que.poll() + ", ");
        }
        
        System.out.println(que.poll() + ">");
    }
}
cs

처음에는 변수 j를 설정하고 j++하며 k와 같을 때 poll하도록 했지만 시간초과가 났다

음 근데 이거는 왜 시간초과가 안날까...? 비슷한 거 같은데

 

프로젝트를 자바로 진행해서 자바가 좀 익숙해졌다

백준 풀이는 자바와 C를 번갈아 가며 할 생각이다.. 나쁘지 않은 것 같다

'프로그래밍 언어 > JAVA' 카테고리의 다른 글

백준 > 1676 팩토리얼 0의 개수  (0) 2022.02.24
백준 > 2798 블랙잭  (0) 2022.02.24
백준 > 2751 수 정렬하기 2  (0) 2022.02.23