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;
}
'코딩테스트 > 백준' 카테고리의 다른 글
[백준][C++]28279번. 덱 2 (0) | 2024.10.04 |
---|---|
[백준][C++]11866번. 요세푸스 문제 0 (1) | 2024.10.04 |
[백준][C++]12789번. 도키도키 산식드리미 (0) | 2024.10.03 |
[백준][C++]4949번. 균형잡힌 세상 (2) | 2024.10.02 |
[백준][C++]9012번. 괄호 (0) | 2024.10.02 |