/
BOJ 10828번 스택.cpp
71 lines (59 loc) · 1.27 KB
/
BOJ 10828번 스택.cpp
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
/**
* 스터디에서 알고리즘 문제 함께 풀어보실 분들
* 여기로 → https://wsapt.github.io/public/
*
* BOJ 10828번 스택
*
* 문제링크 : https://www.acmicpc.net/problem/10828
*
* CPP소스 : https://bit.ly/2Q86YqA
*/
#include <iostream>
#include <stack>
using namespace std;
void solve();
void solve() {
int N;
cin >> N;
stack<int> st;
for (int i = 0; i < N; ++i) {
string cmd;
cin >> cmd;
if ("push" == cmd) {
int v;
cin >> v;
st.push(v);
}
if ("pop" == cmd) {
if (st.size() > 0) {
int v = st.top();
cout << v << '\n';
st.pop();
}
else {
cout << -1 << '\n';
}
}
if ("size" == cmd) {
cout << st.size() << '\n';
}
if ("empty" == cmd) {
cout << (st.size() == 0) << '\n';
}
if ("top" == cmd) {
if (st.size() > 0) {
cout << st.top() << '\n';
}
else {
cout << -1 << '\n';
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
}