본문 바로가기
알고리즘 문제풀이/백준

[알고리즘/백준] 2164 카드2(자바, 큐(Queue))

by jeonghaemin 2021. 9. 25.
728x90

문제

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

풀이 코드

  • 큐(Queue) 자료구조를 사용하여 풀이
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

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<>();

        for (int i = 1; i <= n; i++) {
            queue.offer(i);
        }

        while (true) {
            if (queue.size() == 1) {
                System.out.println(queue.poll());
                return;
            }

            queue.poll();
            queue.offer(queue.poll());
        }
    }
}

댓글