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

[JAVA][C++]백준_2164_카드2

by 박 현 황 2021. 2. 10.

 

문제링크

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

 

 

 

 

자바

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

public class Main_2164 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Queue<Integer> q = new LinkedList<>();
		
		int N = sc.nextInt();
		sc.close();
		
		for(int i=1;i<=N;i++)
			q.offer(i);
		
		while(q.size() > 1) {
			q.poll();
			q.offer(q.poll());
		}
		System.out.println(q.peek());
	}
}

 

 

C++

 

#include <iostream>
#include <queue>
using namespace std;

int main(){
    int N,num;
    queue<int> q;
    cin>>N;
    for(int i=0;i<N;i++){
        q.push(i+1);
    }

    while(1){
        num = q.front(); //젤 위에 있는거 빼기
        q.pop();
        if(q.empty()){
            cout<<num;
            break;
        }
        num = q.front();
        q.push(num);
        q.pop();//front빼서 뒤로 넣은거
    }
    return 0;
}