백준1158: 요세푸스 문제
문제
https://www.acmicpc.net/problem/1158
문제 풀이
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.*;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int cnt = 0;
int idx = 0;
Queue queue = new LinkedList();
int[] arr = new int[n];
IntStream.range(1, n + 1).forEach(queue::offer);
while (!queue.isEmpty()) {
int data = (int) queue.remove();
cnt += 1;
if (cnt % k == 0) {
arr[idx] = data;
idx++;
} else {
queue.add(data);
}
}
System.out.print("<");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
if (i < arr.length - 1) {
System.out.print(", ");
}
}
System.out.print(">");
}
}
댓글남기기