[알고리즘]/백준

백준 1158 자바 - 요세푸스 문제

broship 2021. 6. 17. 08:22

문제


 

 

 

문제해결


import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class B1158 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        Queue<Integer> q = new LinkedList<>();
        for (int i = 1; i <= n; i++) {
            q.offer(i);
        }

        System.out.print("<");
        while (!q.isEmpty()){
            for (int i = 1; i < k; i++) {
                q.offer(q.poll());
            }
            System.out.print(q.poll());
            if (!q.isEmpty()) System.out.print(", ");
        }
        System.out.print(">");
    }
}

- 1부터 n까지의 숫자를 큐에 담은 후 k번째 전까지는 꺼내서 다시 담고 k번째는 꺼내서 출력하면 된다

'[알고리즘] > 백준' 카테고리의 다른 글

백준 17413 자바 - 단어 뒤집기2  (0) 2021.06.18
백준 10866 자바 - 덱  (0) 2021.06.18
백준 10845 자바 - 큐  (0) 2021.06.16
백준 1406 자바 - 에디터  (0) 2021.06.15
백준 1874 자바 - 스택 수열  (0) 2021.06.14