백준 문제풀이 (C++)/자료구조
[백준] 10828번 스택 C++ 풀이법
haula
2020. 9. 2. 12:01
https://www.acmicpc.net/problem/10828
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 | #include<iostream> #include<stack> #include<string> using namespace std; int main() { stack <int> st; int N; string order; cin >> N; for (int i = 0; i < N; i++) { cin >> order; if (order == "push") { int X; cin >> X; st.push(X); } else if (order == "pop") { if (st.empty() == true) { cout << -1 << "\n"; } else { cout << st.top() << "\n"; st.pop(); } } else if (order == "size") { cout << st.size() << "\n"; } else if (order == "empty") { cout << st.empty() << "\n"; } else if (order == "top") { if (st.empty() == true) cout << -1 << "\n"; else cout << st.top() << "\n"; } } } | cs |