[Silver 4] 카드 섞기

devKyoun·2023년 5월 18일
0
post-thumbnail

문제출처


💡IDEA

  • 맨 위에 카드를 빼서 다시 밑으로 넣는 작업은 큐의 성질과 어울린다

⚙️Build

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

public class App9 {
	

	public static void main(String[] args){
		Scanner scanner = new Scanner(System.in);

		//큐 생성
		Queue<Integer> que = new LinkedList<>();

		int N = scanner.nextInt();

		scanner.close();

		//큐 삽입
		for(int i=0; i<N; i++){
			que.add(i+1);
		}
		
		//카드가 한장 남을때 까지 반복
		while(que.size() != 1){
			que.remove();
			int back = que.poll();
			que.add(back);
		}

		System.out.println(que.peek());
	}
}




profile
Game Developer

0개의 댓글