PS

BOJ 28278 : 스택 2

lickelon 2025. 1. 28. 22:39
  • 문제 링크 : boj.kr/28278
  • 난이도 : S4
  • 태그 : 스택

코드

#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()

#define INF 0x7FFFFFFF

using namespace std;

using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;

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

    int n;
    cin >> n;

    stack<int> _st;
    while(n--) {
        int q;
        cin >> q;
        if(q == 1) {
            int input;
            cin >> input;
            _st.push(input);
        }
        if(q == 2) {
            cout << (_st.empty() ? -1 : _st.top()) << "\n";
            if(!_st.empty()) _st.pop();
        }
        if(q == 3) {
            cout << _st.size() << "\n";
        }
        if(q == 4) {
            cout << (int)_st.empty() << "\n";
        }
        if(q == 5) {
            cout << (_st.empty() ? -1 : _st.top()) << "\n";
        }
    }

    return 0;
}

풀이

쿼리에 맞춰 스택의 메서드들을 잘 사용해주면 되는 기본문제이다.

728x90