#include <iostream>#include <stack>#include <string>using namespace std;int main(){stack <int> st;string str;int testcase;cin >> testcase;for (int i = 0; i < testcase; i++){cin >> str;if (str == "push"){int num;cin >> num;st.push(num);}else if (str == "pop"){if (!st.empty()){cout << st.top() << endl;st.pop();}elsecout << "-1" << endl;}else if (str == "size"){cout << st.size() << endl;}else if (str == "empty"){if (st.empty())cout << "1" << endl;elsecout << "0" << endl;}else if (str == "top"){if (!st.empty())cout << st.top() << endl;elsecout << "-1" << endl;}}return 0;}
'백준' 카테고리의 다른 글
[2504] 괄호의 값 (0) | 2018.07.22 |
---|---|
[9012] 괄호 (0) | 2018.07.22 |
[4948] 베르트랑 공준 (0) | 2018.07.21 |
[1929] 소수 구하기 (0) | 2018.07.17 |
[2581] 소수 (0) | 2018.07.17 |