Language/C++

[C++ / 백준 10828] 스택

ej503 2021. 8. 9. 12:11

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

using namespace std;

int main(void) {
    int n;
    cin >> n;

    stack<int> st; 
    string str;
   

    for (int i = 0; i < n; 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();
            }
            else {
                cout << "-1" << endl;
            }

        }
        else if (str == "size") {        

            cout << st.size() << endl;

        }
        else if (str == "empty") {    

            if (st.empty()) {
                cout << "1" << endl;
            }
            else {
                cout << "0" << endl;
            }

        }
        else if (str == "top") {        

            if (!st.empty()) {
                cout << st.top() << endl;
            }
            else {
                cout << "-1" << endl;
            }

        }

    }
    return 0;
}


'Language > C++' 카테고리의 다른 글

[C++ / 백준 2164] 카드2  (0) 2021.08.13
[C++ / 백준 18258] 큐 2  (0) 2021.08.11
[C++ / 백준 10872] 팩토리얼  (0) 2021.08.05
[C++ / 백준 1427] 소트인사이드  (0) 2021.08.04
[C++ / 백준 10989] 수 정렬하기 3  (0) 2021.08.03