Algorithm/백준

C++ 알고리즘 - 백준 28728 스택 2

마루설아 2025. 1. 12. 19:28

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;
		}
	}
}