
ArrayList의 인덱스는 0부터 시작하니 index 변수를 하나 만들고 0으로 초기화해준다.
n이 7이고 k가 3일 경우 제일 처음 아래와 같은 상태가 된다.
List[1, 2, 3, 4, 5, 6, 7]
첫 번째 반복문에서 index = (0+3-1)%7 = 2이다.
al.remove(index)를 하면 3이 출력되고 List 상태는 아래와 같은 상태가 된다.
[1, 2, 4, 5, 6, 7]
두 번째 반복문에서 index = (2+3-1)%6 = 4이다.
al.remove(index)를 하면 6이 출력되고 List 상태는 아래와 같은 상태가 된다.
List가 빌때까지 반복하면
최종적으로 <3, 6, 2, 7, 5, 1, 4>이 출력된다.
import java.util.*;
import java.io.*;
class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
ArrayList<Integer> al = new ArrayList<>();
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
for(int i=1;i<=n;i++){
al.add(i);
}
int index = 0;
sb.append("<");
while(!al.isEmpty()){
index = (index+k-1)%al.size();
sb.append(al.remove(index));
if(!al.isEmpty()){
sb.append(", ");
}
}
sb.append(">");
System.out.print(sb);
}
}

참고
https://st-lab.tistory.com/197
https://nukoori.tistory.com/39