Study/Algorithm
[백준] 스택(10828번) - C++
developer_ny
2021. 4. 17. 03:58
문제
입력
출력
소스 코드
#include <iostream>
#include <stack>
using namespace std;
int main() {
int n; cin >> n;
stack<int> s;
for(int i=0;i<n;i++) {
string command; cin >> command;
if(command == "push") {
int num; cin >> num;
s.push(num);
}
else if(command == "pop") {
if(s.empty()) cout << -1 << "\n";
else {
cout << s.top() << "\n";
s.pop();
}
}
else if(command == "size") cout << s.size() << "\n";
else if(command == "empty") {
if(s.empty()) cout << 1 <<"\n";
else cout << 0 << "\n";
}
else {
if(s.empty()) cout << -1 << "\n";
else {
cout << s.top() << "\n";
}
}
}
return 0;
}