BOJ 28278 스택 2

1 개요[ | ]

BOJ 28278 스택 2


2 C++[ | ]

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    int N;
    cin >> N;

    int command, X;
    stack<int> s;
    for(int i=0; i<N; i++) {
        cin >> command;
        switch(command) {
            case 1:
                cin >> X;
                s.push(X);
                continue;
            case 2:
                if(s.empty()) {
                    cout << "-1\n";
                    continue;
                }
                cout << s.top() << "\n";
                s.pop();
                continue;
            case 3:
                cout << s.size() << "\n";
                continue;
            case 4:
                cout << s.empty() << "\n";
                continue;
            case 5:
                if(s.empty()) {
                    cout << "-1\n";
                    continue;
                }
                cout << s.top() << "\n";
                continue;
        }
    }
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}