728x90
큐 문제입니다.
1. 1~n까지의 수를 큐에 넣습니다.
2. q.pop(), 먼저 들어온 것을 제거합니다.
3. q.push(q.front()), 앞에 있는 것을 뒤에 넣습니다.
4. q.pop(), 앞에 있는 것을 제거합니다.(뒤로 옮긴 상태)
5. q.size()가 1이 될때까지 2~4 반복
6. q.front() 출력
#include <iostream>
#include <cstring>
#include <queue>
#include <string>
#include <algorithm>
using namespace std;
int main(int argc, char *argv[])
{
int n;
queue<int> q;
cin >> n;
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() << endl;
return 0;
}
'알고리즘' 카테고리의 다른 글
[C++] 백준 4949번 균형잡힌 세상 (0) | 2019.09.04 |
---|---|
[C++] 백준 9012번 괄호 (0) | 2019.09.04 |
[C++] 백준 10773번 제로 (0) | 2019.09.04 |
[C++] 백준 2004번 조합 0의 개수 (0) | 2019.09.04 |
[C++] 백준 1676번 팩토리얼 0의 개수 (0) | 2019.09.04 |