Algorithm/BOJ
[BOJ]10828 - 스택 (C++)
꾸지새미언니
2022. 1. 4. 17:47
문제
https://www.acmicpc.net/problem/10828
코드
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main(void){
cin.tie(0);cout.tie(0);
ios_base::sync_with_stdio(false);
stack<int> s;
int N;
cin >> N;
for (int i=0; i<N; i++){
string command ;
cin >> command;
if (command == "push"){
int n;
cin >> n;
s.push(n);
} 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 (command == "top"){
if(s.empty()) cout << -1 << "\n";
else{
cout << s.top() << "\n";
}
}
}
}
풀이
이 문제는 스택으로 알고리즘 푸는 연습을 하는 문제이다.
문자열로 명령문을 받고 각각의 명령에 해당하는 스택에 관한 함수를 호출한다.