Algorithm/Simulation

Baekjoon 2164 카드 2

GirlFriend_Yerin 2019. 11. 11. 10:20

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

결과 4124KB / 4ms

언어 C++71

 

풀이

전형적인 시뮬레이션 문제

사간제한도 2초에 n도 최대 20만밖에 안된다.

n까지 모든 값을 queue에 넣고 1개가 될 때 까지 무한 반복하면 된다.

 

소스코드

#include <iostream>
#include <queue>

using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	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;
}