algorithm'''problem solve

[백준]10828-스택(설명X)

JunHwa Park 2020. 2. 1. 19:40
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
#include <iostream>
#include <stack>
using namespace std;
 
int main() {
    int command;
    cin >> command;
    stack<int> stk;
    for (size_t i = 0; i < command; i++) {
        string str;
        cin >> str;
        if (!str.compare("pop")) {
            int size = stk.size();
            if (size > 0) {
                cout << stk.top() << endl;
                stk.pop();
            }
            else
                cout << -1 << endl;
        }
        else if (!str.compare("size")) {
            cout << stk.size() << endl;
        }
        else if (!str.compare("empty")) {
            if (stk.empty())
                cout << 1 << endl;
            else
                cout << 0 << endl;
        }
        else if (!str.compare("top")) {
            int size = stk.size();
            if (size > 0)
                cout << stk.top() << endl;
            else
                cout << -1 << endl;
        }
        else {
            int num;
            cin >> num;
            stk.push(num);
        }
    }
    return 0;
}
cs