본문 바로가기
Algorithm & SQL/BAEKJOON

백준 JAVA 11866 요세푸스 문제0

by YoonJong 2022. 7. 21.
728x90

package BAEKJOON.Silver.Ⅴ;

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

public class NO11866 {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();

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

        Queue<Integer> queue = new LinkedList<>();

        for (int i = 1; i <= n; i++) {
            queue.offer(i);
        }
        sb.append("<");
        while (queue.size() != 1) {
            for (int i = 1; i <= k; i++) {
                if (i < k) {
                    queue.offer(queue.poll());
                } else {
                    sb.append(queue.poll() + ", ");
                }
            }
        }
        System.out.print(sb);
        System.out.print(queue.poll());
        System.out.print(">");

    }
}
728x90

'Algorithm & SQL > BAEKJOON' 카테고리의 다른 글

백준 JAVA 15651 N과 M(3)  (0) 2022.07.26
백준 JAVA 15649 N과 M(1)  (0) 2022.07.26
백준 JAVA 1026 보물  (0) 2022.07.19
백준 JAVA 9012 괄호  (0) 2022.07.19
백준 JAVA 1065 한수  (0) 2022.07.19

댓글