11866번: 요세푸스 문제 0 (acmicpc.net)
11866번: 요세푸스 문제 0
첫째 줄에 N과 K가 빈 칸을 사이에 두고 순서대로 주어진다. (1 ≤ K ≤ N ≤ 1,000)
www.acmicpc.net
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int k = input.nextInt();
Queue<Integer> Josephus = new LinkedList<>();
for (int i = 1; i<=n; i++) Josephus.add(i);
StringBuilder output = new StringBuilder("<");
int cnt = 0; int index = 1;
while (cnt < n) {
if (index % k == 0) {
output.append(Josephus.remove());
if (cnt!=n-1) output.append(", ");
else output.append(">");
cnt++;
}
else {
Josephus.add(Josephus.remove());
}
index++;
}
System.out.println(output);
}
}
'CodingTest > 백준' 카테고리의 다른 글
[JAVA] 백준 - 1018번: 체스판 다시 칠하기 | 브루트포스 알고리즘 (0) | 2023.10.03 |
---|---|
[JAVA] 백준 - 11650: 좌표 정렬하기 (0) | 2023.09.26 |
[JAVA] 백준 - 10814번: 나이순 정렬 | Arrays.sort() (0) | 2023.09.25 |
[c++] 백준 - 10971: 외판원 순회2 (0) | 2023.07.30 |
[c++] 백준 - 1759번: 암호 만들기 | 재귀함수 (0) | 2023.07.20 |