백준 10845 - 큐 (Silver IV)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// 백준 10845 - 큐 (Silver IV)
// https://www.acmicpc.net/problem/10845
// 문제 설명
// 정수를 저장하는 큐를 구현한 다음, 입력으로 주어지는 명령을 처리하는 프로그램
// 입력
// 첫째 줄: 명령의 수 N (1 ≤ N ≤ 10,000)
// 각 줄: 명령어 (push X / pop / size / empty / front / back)
// 출력
// push를 제외한 각 명령어마다 해당하는 값 출력
// pop / front / back: 큐가 비어있으면 -1 출력
// empty: 비어있으면 1, 아니면 0 출력
// size: 큐에 들어있는 정수의 개수 출력
// 입출력 예
// 입력: 출력:
// push 1
// push 2
// front 1
// back 2
// size 2
// empty 0
// pop 1
// pop 2
// pop -1
// size 0
// empty 1
// pop -1
// push 3
// empty 0
// front 3
#include <iostream>
#include <queue>
#include <string>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
queue<int> q;
string cmd;
for (int i = 0; i < n; i++)
{
cin >> cmd;
if (cmd == "push")
{
int x;
cin >> x;
q.push(x);
}
else if (cmd == "pop")
{
// 풀이 작성
if(!q.empty() )
{
cout << q.front() << endl;
q.pop();
}
else
{
cout << -1 << endl;
}
}
else if (cmd == "size")
{
cout << q.size() << endl;
}
else if (cmd == "empty")
{
if(q.empty()) cout << 1 << endl;
else cout << 0 << endl;
}
else if (cmd == "front")
{
if(!q.empty() )
{
cout << q.front() << endl;
}
else
{
cout << -1 << endl;
}
}
else if (cmd == "back")
{
if(!q.empty() )
{
cout << q.back() << endl;
}
else
{
cout << -1 << endl;
}
}
}
return 0;
}
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.