[C++] 백준 2164번 카드2

xyzw·2025년 3월 14일
0

algorithm

목록 보기
59/61

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

풀이

문제의 규칙에 따라 큐에서 pop하고 push하며, 큐의 크기가 1이면 반복을 중단한다.

코드

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

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    
    int n;
    cin >> n;
    
    queue<int> q;
    for(int i=1; i<=n; i++) q.push(i);
    
    while(q.size() != 1) {
        q.pop();
        q.push(q.front());
        q.pop();
    }
    
    cout << q.front();
    
    return 0;
}

0개의 댓글