백준 2161번(Java)

박은지·2025년 2월 7일
0

백준

목록 보기
25/89
post-thumbnail

import java.io.*;
import java.util.*;

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> q = new LinkedList<>();
		
		for(int i=1; i<=N; i++) {
			q.offer(i); // 큐에 값 삽입
		}
		
		while(q.size() > 1) {
			q.poll(); // 맨 앞에 값 버림
			q.offer(q.poll()); // 맨 앞에 값 버림 & 버린 값을 맨 뒤에 삽입
		}
		
		System.out.println(q.poll());
	}
}
profile
백엔드 개발자가 되고싶은 eunzi😊

0개의 댓글