Published 2020. 12. 29. 05:32
 

10828번: 스택

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

www.acmicpc.net

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

int main() {
    using namespace std; ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);

    int n;
    cin >> n;

    stack<int> s;
    while (n--) {
        string cmd;
        cin >> cmd;
        if (cmd == "push") {
            int num;
            cin >> num;
            s.push(num);
        }
        else if (cmd == "top") {
            cout << (s.empty() ? -1 : s.top()) << '\n';
        }
        else if (cmd == "size") {
            cout << s.size() << '\n';
        }
        else if (cmd == "empty") {
            cout << s.empty() << '\n';
        }
        else if (cmd == "pop") {
            cout << (s.empty() ? -1 : s.top()) << '\n';
            if (!s.empty()) {
                s.pop();
            }
        }
    }
    return 0;
}

'Problem set' 카테고리의 다른 글

[백준] 10845: 큐  (0) 2020.12.29
[백준] 10844: 쉬운 계단 수  (0) 2020.12.29
[백준] 10818: 최소, 최대  (0) 2020.12.29
[백준] 10799: 쇠막대기  (0) 2020.12.29
[백준] 10430: 나머지  (0) 2020.12.29
복사했습니다!