문제
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();
}
}
'자바 > 백준' 카테고리의 다른 글
[백준 11718] 그대로 출력하기 (0) | 2022.09.08 |
---|---|
[백준 10769] 행복한지 슬픈지 (0) | 2022.09.08 |
[백준 6502] 동혁 피자 (0) | 2022.09.07 |
[백준 2440] 별 찍기 - 3 (0) | 2022.09.06 |
[백준 2083] 럭비 클럽 (0) | 2022.09.06 |