Algorithm/문제

41. 공주 구하기

챛채 2023. 10. 10. 15:14

import java.util.*;

public class Main {

    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){
       Main T = new Main();
       Scanner kb = new Scanner(System.in);
       int n = kb.nextInt();
       int k = kb.nextInt();
       System.out.println(T.solution(n,k));
   }
}