본문 바로가기
백준(C++)/스택_큐_덱

[BOJ/C++]2164번_카드2

by 코잼민 2024. 7. 4.

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