[C++] 백준 10828 : 스택

Kim Nahyeong·2022년 1월 3일
0

백준

목록 보기
9/157

#include <iostream>
#include <stack> // stl 스택

using namespace std;

int main(void){
    int N, num;
    stack<int> stack; // 스택 선언
    string str;

    scanf("%d", &N);

    for(int i = 0; i < N; i++){
        cin >> str;

        if(str == "push"){
            scanf("%d", &num);
            stack.push(num);
        } else if(str == "pop"){
            if(stack.empty()){
                printf("-1\n");
            } else {
                cout << stack.top() << endl;
                stack.pop();
            }
        } else if(str == "size"){
            cout << stack.size() << endl;
        } else if(str == "empty"){
            if(stack.empty()){
                printf("1\n");
            } else {
                printf("0\n");
            }
        } else if(str == "top"){
            if(stack.empty()){
                printf("-1\n");
            } else {
                cout << stack.top() << endl;
            }
        }
    }

    return 0;
}

오늘의 키포인트

  • c++은 stack 라이브러리를 가지고 잇다. 그걸 쓰면 바로바로 해결 완료

0개의 댓글