First In First Out(FIFO),
public int solution(int n, int k){
int answer= 0;
Queue<Integer> Q = new LinkedList<>();
for(int i=1;i<=n;i++)
Q.offer(i);
while(!Q.isEmpty()){
for(int i=1;i<k;i++)
Q.offer(Q.poll());
// K=3이면 Q의 1,2번째를 꺼내고 뒤에 저장한다.
Q.poll(); // Q의 맨 앞 index를 제거한다.
if(Q.size() == 1)
answer = Q.poll();
}
return answer;
}
public static void(String[] args){
Main41 T = new Main41();
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
int k = kb.nextInt();
System.out.println(T.solution(n, k));
}
package algolecture;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main41 {
public int solution(int n, int k) {
int answer = 0;
Queue<Integer> Q = new LinkedList<>();
for(int i=1;i<=n;i++)
Q.offer(i);
while(!Q.isEmpty()){
for(int i=1;i<k;i++)
Q.offer(Q.poll());
Q.poll();
if(Q.size()==1)
answer = Q.poll();
}
return answer;
}
public static void main(String[] args) {
Main41 T = new Main41();
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
int k = kb.nextInt();
System.out.println(T.solution(n ,k));
}
}