백준 10828번: 스택

Se0ng_1l·2022년 7월 15일
0

백준

목록 보기
33/40

https://www.acmicpc.net/problem/10828

문제접근

10845번 큐 문제 푸는 방식을 이용해 스택을 구현하면 된다.

#include <iostream>
#include <stack>
using namespace std;

int main()
{
    int num;
    cin >> num;
    stack<int> s;
    for(int i = 0; i < num; i++)
    {
        string str;
        cin >> str;
        int temp;
        if(str == "push"){
            cin >> temp;
            s.push(temp);
        }
        else if(str == "pop")
        {
            if(s.empty())
                cout << -1 << endl;
            else{
                cout << s.top() << endl;
                s.pop();
            }
        }
        else if(str == "size")
        {
            cout << s.size() << endl;
        }
        else if(str == "empty")
        {
            cout << s.empty() << endl;
        }
        else if(str == "top")
        {
            if(s.empty())
                cout << -1 << endl;
            else{
                cout << s.top() << endl;
            }
        }
    }
}
profile
치타가 되고 싶은 취준생

0개의 댓글