알고리즘
[C++] 백준 10828번 스택
J3SUNG
2019. 8. 26. 23:25
728x90
스택 문제입니다.
push, top, size, empty, pop에 대한 입력을 받으면
그에 맞는 출력을 하는 문제입니다.
간단하게 5가지에 대한 함수를 만들어주고
각각 실행될때의 결과값을 그대로 실행해주면 됩니다.
#include <iostream>
#include <stack>
#include <string>
using namespace std;
stack<int> s;
void push(int x)
{
s.push(x);
}
int pop()
{
if (!s.empty()) {
int x = s.top();
s.pop();
return x;
}
return -1;
}
int size()
{
return s.size();
}
int empty()
{
if (s.empty()) {
return 1;
}
return 0;
}
int top()
{
if (!s.empty()) {
return s.top();
}
return -1;
}
int main(int argc, char *argv[])
{
int ts;
string str;
cin >> ts;
while (ts--) {
int n;
int tmp;
cin >> str;
if (str == "push") {
cin >> n;
push(n);
}
else if (str == "pop") {
tmp = pop();
if( tmp == -1) {
cout << "-1" << endl;
}
else {
cout << tmp << endl;
}
}
else if (str == "size") {
cout << size() << endl;
}
else if (str == "empty") {
cout << empty() << endl;
}
else if (str == "top") {
tmp = top();
if (tmp == -1) {
cout << "-1" << endl;
}
else {
cout << tmp << endl;
}
}
}
return 0;
}