C++
(C++) [백준] Stack
JJeongHyun
2023. 1. 10. 14:25
반응형
https://www.acmicpc.net/problem/10828
10828번: 스택
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
요약 )
- 첫 번째로 입력할 수는 실행할 명령어의 수이다.
- 원하는 스택에 대한 명령(출력하고자 하는)을 작성하면 된다
- 정수의 범위는 1 이상 100,000 이하이다
- 문제에서 나오지 않은 명령은 하지 않는다
#include <iostream>
#include <string>
#include <stack>
using namespace std;
stack<int> s;
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++) {
string ans;
cin >> ans;
if (ans == "push") {
int x;
cin >> x;
s.push(x);
}
else if (ans == "top") {
if (s.empty()) cout << "-1" << "\n";
else cout << s.top() << "\n";
}
else if (ans == "pop") {
if (s.empty()) cout << "-1" << "\n";
else {
cout << s.top() << "\n";
s.pop();
}
}
else if (ans == "empty") {
cout << s.empty() << "\n";
}
else {
cout << s.size() << "\n";
}
}
return 0;
}