큐 - 카드게임

김동헌·2023년 10월 31일

Algorithm

목록 보기
10/13
post-thumbnail

🔎 문제

https://www.acmicpc.net/problem/2164


🔍 문제 분석

해당 과정에서 Queue를 사용할 때 poll()은 2번하게 된다.
poll() 1번 후, add(Queue.poll())과 같이 현재 큐에서 poll을 함과 동시에 큐에 꺼낸 값을 넣어주는 방식이다.

스택과 큐 개념 설명에서 개념을 이해했다면 쉬운 문제로 다른 분석 없이 풀 수 있을 것으로 예상해서 다른 분석은 하지 않고 바로 구현하였다.


👀 코드


import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class BackJoon_2164 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        Queue<Integer> queue = new LinkedList<>();
        for(int i = 1; i <= N; i++) {
            queue.add(i);
        }

        while(queue.size() > 1){
            queue.poll();
            queue.add(queue.poll());
        }
        System.out.println(queue.poll());
    }
}
profile
백엔드 기록 공간😁

0개의 댓글