알고리즘 문제
[C/C++ 백준 10828번] 스택 (Silver 4)
새파란 공대생
2020. 8. 20. 12:41
https://www.acmicpc.net/problem/10828
10828번: 스택
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지 �
www.acmicpc.net
STL에서 제공하는 stack 라이브러리를 이용해 주어진 조건대로 구현하면 된다.
#include <cstdio>
#include <stack>
#include <cstring>
using namespace std;
int main(void){
int N, x;
stack <int> s;
char str[50];
char str1[50]="push";
char str2[50]="pop";
char str3[50]="size";
char str4[50]="empty";
char str5[50]="top";
scanf("%d", &N);
for(int i=0; i<N; i++){
scanf(" %s",str);
if(strcmp(str, str1)==0){
scanf("%d", &x);
s.push(x);
}
if(strcmp(str, str2)==0){
if(s.empty())
printf("-1\n");
else{
printf("%d\n",s.top());
s.pop();
}
}
if(strcmp(str, str3)==0)
printf("%d\n",s.size());
if(strcmp(str, str4)==0)
printf("%d\n",s.empty());
if(strcmp(str, str5)==0){
if(s.empty())
printf("-1\n");
else
printf("%d\n",s.top());
}
}
}