본문 바로가기

Algorithm/스택과 큐

백준 카드2(2164) C++ 풀이

알고리즘 분류

자료구조

문제 해결 아이디어

큐를 이용하여 큐의 사이즈가 1이 될 때 까지 pop() 한 후 top()을 임시 저장한 후 pop()하여
임시 저장한 값을 다시 큐에넣는 과정을 반복하면 되는 문제이다.

코드

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


int n;
queue<int> q;
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		q.push(i);
	}

	while (q.size() > 1)
	{
		q.pop();
		int top = q.front();
		q.pop();
		q.push(top);
	}

	cout << q.front();
}