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

 

문제에서 설명하는대로 제일 앞에 수를 버리고 제일 위에 수를 제일 뒤로 보내는 과정을 queue의 크기가 1일때 까지 반복해주면 된다.

 

정답코드

#include <iostream>
#include <queue>

using namespace std;

int main()
{
	int n;

	cin >> n;

	queue<int> q;

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

	while (q.size() != 1)
	{
		int x;
		q.pop();
		x = q.front();
		q.pop();
		q.push(x);
	}

	cout << q.front() << endl;

	return 0;
}

 

+ Recent posts