728x90
//백준 10828 - 스택
#include <stack>
#include <iostream>
#include <string>
using namespace std;
int main(){
int n;
cin >> n; // 명령 수 입력
stack<int> stk;
string command;
int num;
while(n--){
cin >> command;;
if(command == "push"){ // 스택에 정수 넣기
cin >> num;
stk.push(num);
}
else if(command == "top"){ // 스택의 가장 위에 있는 정수 출력
if(stk.empty()) // 스택에 들어있는 정수가 없는 경우
cout << "-1\n";
else
cout << stk.top() << "\n";
}
else if(command == "size"){ // 스택에 들어있는 정수 개수 출력
cout << stk.size() << "\n";
}
else if(command == "empty"){ // 스택 비어있으면 1, 아니면 0
if(stk.empty()) cout << "1\n";
else cout << "0\n";
}
else if(command == "pop"){ // 스택에서 가장 위에 있는 정수 빼고 출력
if(stk.empty()) // 스택에 들어있는 정수가 없는 경우
cout << "-1\n";
else {
cout << stk.top() << "\n";
stk.pop();
}
}
}
return 0;
}
'Algorithm > Baekjoon' 카테고리의 다른 글
[백준 11729] C++ / 하노이 탑 (1) | 2024.07.20 |
---|---|
[백준 24460] C++ / 다차원 배열 vector (1) | 2024.07.20 |
[백준 10988] C++ - 팰린드롬 확인 (0) | 2024.07.16 |
[백준 10870] C++ 피보나치 수 (0) | 2024.07.15 |
[백준 17478] c++ - cout 대신 printf문 사용 / c_str() (0) | 2024.07.15 |