티스토리 뷰

algorithm/백준

[c++]백준 10828: 스택

bouble12 2020. 11. 21. 10:23

www.acmicpc.net/problem/10828

 

10828번: 스택

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net

#include <iostream>
#include <stack>
#include <string>

using namespace std;

int main() {
    stack<int> st;
    int n;
    cin >> n;
    while (n--) {
        string cmd;
        cin >> cmd;
        if (cmd == "push") {
            int x;
            cin >> x;
            st.push(x);
        } else if (cmd == "pop") {
            if (st.empty()) cout << -1 << endl;
            else {
                cout << st.top() << endl;
                st.pop();
            }
        } else if (cmd == "size") {
            cout << st.size() << endl;
        } else if (cmd == "empty") {
            if (st.empty())cout << 1 << endl;
            else cout << 0 << endl;
        } else if (cmd == "top") {
            if (st.empty()) cout << -1 << endl;
            else cout << st.top() << endl;
        }
    }
}
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/10   »
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
글 보관함