Algorithm

[백준-2164번/Java] 카드2

1984 2022. 11. 18. 00:08

* Queue 를 이용하여 풀었다.

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

public class Main {

	public static void main(String[] args) throws IOException {
		Scanner in = new Scanner(System.in);
		int N = in.nextInt();

		// Queue 초기화
		Queue<Integer> q = new LinkedList<Integer>();
		for (int i = 0; i < N; i++) {
			q.add(i + 1);
		}

		while (q.size() > 1) {
			// 제일 위 카드 버리기
			q.poll();
			// 제일 위 카드 제일 아래로 옮기기
			q.add(q.poll());
		}
		
		System.out.println(q.poll());
	}
}
728x90