https://www.acmicpc.net/problem/28278
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
void CPP_INIT() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int main(void) {
CPP_INIT();
int input;
int select;
int num;
int n;
stack<int> st;
cin >> input;
for (int i = 0; i < input; i++) {
cin >> select;
switch (select) {
case 1:
cin >> num;
st.push(num);
break;
case 2:
if (st.empty()) {
cout << -1 << endl;
break;
}
n = st.top();
st.pop();
cout << n << endl;
break;
case 3:
cout << st.size() << endl;
break;
case 4:
if (st.empty()) cout << 1 << endl;
else cout << 0 << endl;
break;
case 5:
if (st.empty()) {
cout << -1 << endl;
break;
}
n = st.top();
cout << n << endl;
break;
}
}
}
'Algorithm > 백준' 카테고리의 다른 글
C++ 알고리즘 - 백준 18528 큐 2 (0) | 2025.01.12 |
---|---|
C++ 알고리즘 - 백준 12789 도키도키 간식드리미 (0) | 2025.01.12 |
C++ 알고리즘 - 백준 17103 골드바흐 파티션 (에라토스테네스의 체) (0) | 2025.01.12 |
C++ 알고리즘 - 백준 4948 베르트랑 공준 (에라토스테네스의 체) (0) | 2025.01.12 |
C++ 알고리즘 - 백준 13909 창문 닫기 (0) | 2025.01.12 |