https://www.acmicpc.net/problem/10828
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
int main(void) {
// C++ Init
ios::sync_with_stdio(false);
cin.tie(NULL);
int input;
int num;
string str;
stack<int> st;
cin >> input;
cin.ignore();
for (int i = 0; i < input; i++) {
getline(cin, str);
if (str == "top") {
if (st.empty()) cout << -1 << endl;
else cout << st.top() << endl;
}
else if (str == "pop") {
if (st.empty()) cout << -1 << endl;
else {
num = st.top();
st.pop();
cout << num << endl;
}
}
else if (str == "size") {
cout << st.size() << endl;
}
else if (str == "empty") {
cout << st.empty() << endl;
}
else if (str.find("push") != -1) {
string number = str.erase(0, 5);
num = stoi(number);
st.push(num);
}
}
}
'알고리즘' 카테고리의 다른 글
C++ 알고리즘 - 백준 11866 요세푸스 문제 0 (0) | 2024.12.31 |
---|---|
C++ 알고리즘 - 백준 10845 큐 (0) | 2024.12.31 |
C++ 알고리즘 - 백준 10773 제로 (0) | 2024.12.31 |
C++ 알고리즘 - 백준 9012 괄호 (0) | 2024.12.31 |
C++ 알고리즘 - 백준 4949 균형잡힌 세상 (0) | 2024.12.31 |