Baekjoon #10828

Baekjoon #10828

3개의 풀이 · C++, Java, Python

문제 원문 보기 ↗

SOLUTION INFO

C++ · main.cpp

main.cpp
#include<bits/stdc++.h>

using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);

    int N; cin >> N;

    stack<int> st;

    for(int i=0;i<N;i++){
        string cmd; cin >> cmd;
        if(cmd == "push") { 
            int X; cin >> X;
            st.push(X);
        }
        else if(cmd == "pop") {
            if(st.empty()) { 
                cout << -1 << '\n';
            }
            else {
                cout << st.top() << '\n';
                st.pop();
            }
        }
        else if(cmd == "size") {
            cout << (int)st.size() << '\n';
        }
        else if(cmd == "empty") {
            cout << st.empty() << '\n';
        }
        else if(cmd == "top") {
            if(st.empty()) {
                cout << -1 << '\n';
            }
            else {
                cout << st.top() << '\n';
            }
        }
    }
}

SOLUTION DESCRIPTION

풀이 설명

등록된 풀이 설명이 없습니다.