[백준 28278] 스택 2

alsry._.112·2023년 10월 2일
0

백준

목록 보기
72/102

🔗문제 풀러가기
단계별로 풀어보기 단계 16의 1번째 문제이다.

문제 분석


코드

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


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

    stack<int> st;

    int n, x, input;
    cin >> n;

    for (int i = 0; i < n; i++)
    {
        cin >> input;
        if (input == 1)
        {
            cin >> x;
            st.push(x);
        }
        else if (input == 2)
        {
            if (st.empty())
            {
                cout << "-1" << "\n";
            }
            else
            {
                cout << st.top() << "\n";
                st.pop();
            }
        }
        else if (input == 3)
        {
            cout << st.size() << "\n";
        }
        else if (input == 4)
        {
            if (st.empty())
            {
                cout << "1" << "\n";
            }
            else
            {
                cout << "0" << "\n";
            }
        }
        else if (input == 5)
        {
            if (st.empty())
            {
                cout << "-1" << "\n";
            }
            else
            {
                cout << st.top() << "\n";
            }
        }
    }
}

주어진 조건에 따라 처리 해주기만 하면 되는 문제이다.

profile
소통해요

0개의 댓글