자바/백준

[백준 1158] 요세푸스 문제

슈슈버거 2022. 9. 7. 15:26

문제

https://www.acmicpc.net/problem/1158

 

1158번: 요세푸스 문제

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

www.acmicpc.net


내가 작성한 코드

import java.io.*;
import java.util.*;

public class 백준1158 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        String[] input = br.readLine().split(" ");
        int N = Integer.parseInt(input[0]);
        int K = Integer.parseInt(input[1]);


        String[] people = new String[N];
        for(int i = 0; i<N; i++){
            people[i] = i+1+"";
        }

        List<String> person = new ArrayList<>(List.of(people));

        int num = N;
        int count = K-1;

        bw.write("<");
        while(num != 1){
            if(count >= person.size()){
                while(count >= person.size()){
                    count -= person.size();
                }
            }

            bw.write(person.get(count)+", ");
            person.remove(count);
            num--;
            count += K-1;
        }

        bw.write(person.get(0)+">");
        bw.close();
    }
}