알고리즘/스택, 큐

[백준] 11866번 요세푸스 문제 #Java

VIPeveloper 2022. 5. 7. 13:45
728x90
반응형
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;


public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine()," ");
        int n = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());

        Queue<Integer> arr = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            arr.offer(i+1);
        }

        StringBuilder sb = new StringBuilder();
        sb.append("<");
        while (arr.size() != 1){

            for (int i = 0; i < m-1; i++) {
                int val = arr.poll();
                arr.offer(val);
            }

            sb.append(arr.poll()).append(", ");
        }
        sb.append(arr.poll()).append(">");
        System.out.println(sb);
    }
}
728x90
반응형