백준 2164 자바(카드 큐)

정호윤·2023년 3월 10일

자바

목록 보기
27/46

괜히 어려운 방법 사용해가면서 애먹다가 큐로 쉽게 풀었다.분명 큐에 대해서 공부했는데 막상 다시 써먹을라니까 잘 모르겠더라.

큐는 선입선출 데이터 구조이다.무조건 먼저 들어온게 먼저 나가게 된다.

이렇게 생겼다.

가장 아래를 날려주고 그 다음거를 제거한뒤 위로 올려주는 과정을 반복하면 된다.

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

class Main{
    public static void main(String[] args) throws IOException{
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        Queue<Integer> queue = new LinkedList<>();
        
        for(int i=0;i<N;i++){
            queue.add(i+1);
        }
        while(queue.size()>1){
            queue.poll();
            int top=queue.poll();
            queue.add(top);
        }
        System.out.println(queue.poll());
    }
}
profile
개발자로 취직을 희망합니다.

0개의 댓글