https://www.acmicpc.net/problem/2164
##문제 풀기 전 내가 알고 있었어야 할 개념:
★★ Queue의 Push(), Pop()
- Push() : [Rear]에서 삽입된다.
- Pop() : [Front]에서 삭제된다.
- ※참고 : Stack은 Push(),Pop() 둘다 [Front]에서 진행된다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
#include<iostream>
#include<queue>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int N;
queue<int> Q;
cin >> N;
//N ~ 1 순서로 Q에 삽입
for (int i = 1; i <= N; i++) {
Q.push(i);//1(f) 2 3 4(r)
}
//Queue의 원소가 1개가 될때까지 과정 반복
while (Q.size()!=1) {
//(1) 1을 버리기 => Pop()
Q.pop();
//(2) front의 2를 Rear로 옮기기
Q.push(Q.front());
Q.pop();
}
//(3) 마지막 남은 1개의 원소 출력
cout << Q.front() << '\n';
return 0;
}
|
cs |
'백준(C++) > 스택_큐_덱' 카테고리의 다른 글
[BOJ/C++]28279번_ 덱 2(Deque) (0) | 2024.07.06 |
---|---|
[BOJ/C++]11866번_ 요세푸스 문제 0 (0) | 2024.07.06 |
[BOJ/C++]18258번_큐 2 (0) | 2024.07.04 |
[BOJ/C++]12789번_도키도키 간식드리미 (1) | 2024.07.03 |
[BOJ/C++]4949번_균형잡힌세상 (2) | 2024.07.03 |