Solved.ac Class 2+
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] split = br.readLine().split(" ");
LinkedList<Integer> ints = new LinkedList<>();
StringBuilder sb = new StringBuilder();
int n = Integer.parseInt(split[0]);
int k = Integer.parseInt(split[1]);
int start = 0;
for (int i = 0; i < n; i++) {
ints.add(i + 1);
}
start = ints.remove((start + k) % ints.size() -1);
sb.append("<").append(start).append(", ");
while (ints.size() != 1) {
int value;
start = start - 1;
start = (start + k) % ints.size();
if (start == 0) {
start = ints.size();
value = ints.removeLast();
} else {
value = ints.remove(start - 1);
}
sb.append(value).append(", ");
}
sb.append(ints.remove()).append(">");
System.out.println(sb);
}
}
IndexOutOfBounds
범위를 넘어버렸다
위처럼 구현해서는 답이 없다고 판단. Queue에서 힌트를 얻음
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] split = br.readLine().split(" ");
Queue<Integer> queue = new LinkedList<>();
StringBuilder sb = new StringBuilder();
int n = Integer.parseInt(split[0]);
int k = Integer.parseInt(split[1]);
for (int i = 0; i < n; i++) {
queue.add(i + 1);
}
sb.append("<");
while (queue.size() != 1) {
for (int i = 1; i < k; i++) {
queue.add(queue.remove());
}
sb.append(queue.remove()).append(", ");
}
sb.append(queue.remove()).append(">");
System.out.println(sb);
}
}
데이터를 빼자마자 뒤로 넣어서 순회시키다가 필요한 때에만 꺼내기
성공