본문 바로가기

BOJ

[백준] 10828번 스택 c++

#include <bits/stdc++.h>

using namespace std;


int main(void) {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int testCase;
    int num;
    string command;
    cin >> testCase;
    stack<int> s;
    for (int i = 0; i < testCase; i++) {
        cin >> command;
        if (command == "push") {
            cin >> num;
            s.push(num);
        }
        else if (command == "pop") {
            if (s.size()) {
                cout << s.top() << "\n";
                s.pop();
            }  
            else
                cout << -1 << "\n";
        }
            
        else if (command == "size")
            cout << s.size() << "\n";
        else if (command == "empty") {
            if (s.empty())
                cout << 1 << "\n";
            else
                cout << 0 << "\n";
        }
        else if (command == "top") {
            if (!s.empty())
                cout << s.top() << "\n";
            else
                cout << -1 << "\n";
        }
    }
}

'BOJ' 카테고리의 다른 글

[백준] 4월 23일 실버 달성  (0) 2020.04.23
[백준] 10845번 큐 c++  (0) 2020.04.23
[백준] 1406번 에디터 C++  (0) 2020.04.22
[백준] 1919번 애너그램 만들기 c++  (0) 2020.04.21
[백준] 11328번 Strfry C++  (0) 2020.04.21