queue 사용
숫자 N 을 입력받아서 차례로 queue.add, size++
while(queue 의 크기가 0보다 클 때 까지) {
queue.poll(); // 맨 위에꺼 버리기
size--;
queue.add(queue.peek()); // 그 다음의 제일 위에 있는 카드를 제일 아래로 넣기
}
return queue.peek();
내 풀이
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
Queue<Integer> queue = new LinkedList<>();
int size = 0;
for(int i = 1; i <= N; i++) {
queue.add(i);
size++;
}
while(size > 1) {
queue.poll();
size--;
queue.add(queue.poll());
}
System.out.println(queue.peek());
}
}
‼️size 변수를 따로 선언하지 않고, queue.size() 메서드 사용할 수 도 있음