개인 공부/코딩테스트
[C++][백준 10828][스택] 스택 :: seoftware
seowit
2020. 2. 21. 17:07
문제
https://www.acmicpc.net/problem/10828
10828번: 스택
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지 않은 명령이 주어지는 경우는 없다.
www.acmicpc.net
소스코드
#include <stack>
#include <iostream>
#include <string>
using namespace std;
stack<int> st;
int main(void) {
int n;
cin >> n;
while (n > 0) {
n--;
string s;
cin >> s;
if (s == "push") {
int x;
cin >> x;
st.push(x);
}
else if (s == "pop") {
if (st.empty()) {
cout << -1 << endl;
}
else {
cout << st.top() << endl;
st.pop();
}
}
else if (s == "size") {
cout << st.size() << endl;
}
else if (s == "empty") {
cout << st.empty() << endl;
}
else if (s == "top") {
if (st.empty()) cout << -1 << endl;
else cout << st.top() << endl;
}
}
return 0;
}
stack<int> st를 while문 안에 넣으면 안됨!!