|
| 1 | +큐를 이용한 방식과 idx를 이용한 방식 두가지를 이용하여 해결하였다. |
| 2 | + |
| 3 | +``` |
| 4 | +package implemention; |
| 5 | +
|
| 6 | +import java.util.*; |
| 7 | +import java.util.stream.Collectors; |
| 8 | +
|
| 9 | +public class b11866 { |
| 10 | +
|
| 11 | + public static void main(String[] args) { |
| 12 | + Scanner sc = new Scanner(System.in); |
| 13 | + String[] str = sc.nextLine().split(" "); |
| 14 | + int N = Integer.parseInt(str[0]); // K번째 사람을 제거 |
| 15 | + int K = Integer.parseInt(str[1]); // N명의 사람이 모두 제거될때까지 |
| 16 | +
|
| 17 | + Queue<Integer> queue = new LinkedList<Integer>(); |
| 18 | + for (int i = 1; i <= N; i++) { |
| 19 | + queue.add(i); |
| 20 | + } |
| 21 | + List<String> result = new ArrayList<>(); |
| 22 | + while(!queue.isEmpty()) { |
| 23 | + for (int i = 1; i < K; i++) { |
| 24 | + queue.add(queue.poll()); |
| 25 | + } |
| 26 | + result.add(String.valueOf(queue.poll())); |
| 27 | + } |
| 28 | + System.out.println(result.stream() |
| 29 | + .collect(Collectors.joining(", ", "<" ,">"))); |
| 30 | + } |
| 31 | +
|
| 32 | +} |
| 33 | +
|
| 34 | +``` |
| 35 | + |
| 36 | +``` |
| 37 | +package implemention; |
| 38 | +
|
| 39 | +import java.util.*; |
| 40 | +import java.util.stream.Collectors; |
| 41 | +
|
| 42 | +public class b11866 { |
| 43 | +
|
| 44 | + public static void main(String[] args) { |
| 45 | + Scanner sc = new Scanner(System.in); |
| 46 | + String[] str = sc.nextLine().split(" "); |
| 47 | + int N = Integer.parseInt(str[0]); // K번째 사람을 제거 |
| 48 | + int K = Integer.parseInt(str[1]) - 1; // N명의 사람이 모두 제거될때까지 |
| 49 | +
|
| 50 | + List<String> list = new ArrayList<>(); |
| 51 | + for (int i = 1; i <= N; i++) { |
| 52 | + list.add(String.valueOf(i)); |
| 53 | + } |
| 54 | + List<String> result = new ArrayList<>(); |
| 55 | + int idx = 0; |
| 56 | + while (!list.isEmpty()) { |
| 57 | + idx = (idx + K) % list.size(); |
| 58 | + result.add(list.get(idx)); |
| 59 | + list.remove(idx); |
| 60 | + } |
| 61 | +
|
| 62 | +
|
| 63 | + System.out.println(result.stream() |
| 64 | + .collect(Collectors.joining(", ", "<" ,">"))); |
| 65 | + } |
| 66 | +
|
| 67 | +} |
| 68 | +``` |
0 commit comments