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
#include <iostream>
#include <stack>
#include <string>
 
using namespace std;
 
int main() {
    int N;
    stack<int> stack1;
    cin >> N;
    for (int i = 0; i < N; i++) {
        string str;
        cin >> str;
        if (str == "push") {
            int a;
            cin >> a;
            stack1.push(a);
        }
        else if (str == "top") {
            if (stack1.empty() == 1)
                cout << "-1" << endl;
            else
                cout << stack1.top() << endl;
        }
        else if (str == "size")
            cout << stack1.size() << endl;
        else if (str == "empty")
            cout << stack1.empty() << endl;
        else if (str == "pop") {
            if (stack1.empty() == 1) {
                cout << "-1" << endl;
            }
            else {
                cout << stack1.top() << endl;
                stack1.pop();
            }
        }
    }
    return 0;
}
cs


+ Recent posts