πŸ“ μ•Œκ³ λ¦¬μ¦˜/BOJ

[C++/BOJ] 2164 : μΉ΄λ“œ2 (Queue)

xxilliant 2024. 11. 17. 11:44
728x90
λ°˜μ‘ν˜•

 

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

 

λ°±μ€€ 싀버4

큐둜 ν’€μ–΄μ•Ό ν•œλ‹€

λ†“μΉ˜κΈ° μ‰¬μš΄ 쑰건 -> 1을 μž…λ ₯ν•  λ•Œ, 0이 μ•„λ‹Œ 1이 λ‚˜μ™€μ•Ό 함. (큐에 μ΅œμ†Œ 1κ°œλŠ” 남아야 ν•œλ‹€)

 

 

λ‚˜μ˜ 풀이

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

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

	int n = 0;
	int answer = 0;
	queue<int> q;

	cin >> n;

	for (int i = 1; i <= n; ++i)
	{
		q.push(i);
	}
	while(1)
	{
		if(q.size()<=1){
			answer = q.front();
			break;
		}
		q.pop();
		int t = q.front();
		q.pop();
		q.push(t);
		
	}
	cout << answer;
	return 0;
}
728x90
λ°˜μ‘ν˜•