본문 바로가기
CodingTest/백준

[JAVA] 백준 - 11866: 요세푸스 문제 0

by Daybreak21 2023. 9. 26.

11866번: 요세푸스 문제 0 (acmicpc.net)

 

11866번: 요세푸스 문제 0

첫째 줄에 N과 K가 빈 칸을 사이에 두고 순서대로 주어진다. (1 ≤ K ≤ N ≤ 1,000)

www.acmicpc.net

 

import java.util.*;

public class Main {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int k = input.nextInt();

        Queue<Integer> Josephus = new LinkedList<>();
        for (int i = 1; i<=n; i++) Josephus.add(i);

        StringBuilder output = new StringBuilder("<");
        int cnt = 0; int index = 1;
        while (cnt < n) {
            if (index % k == 0) {
                output.append(Josephus.remove());
                if (cnt!=n-1) output.append(", ");
                else output.append(">");
                cnt++;
            }
            else {
                Josephus.add(Josephus.remove());
            }
            index++;
        }

        System.out.println(output);

    }
}