/*#include <stdio.h>
int top=-1;
char stack[300]={};
void push(char n)
{
if(top==300) return;
stack[++top]=n;
}
char pop()
{
return stack[top--];
}
int main()
{
int n,b=0;
char str[201]={};
scanf("%d %s",&n,str);
for(int i=n-1; i>=0; i--){
push(str[i]);
b+=1;
if(b%3==0 && i!=0)
{
push(',');
}
}
while(top!=-1){
printf("%c",pop());
}
return 0;
}
#include<stdio.h>
int top=-1;
int stack[100000]={};
void push(int n)
{
if(top==100000) return;
stack[++top]=n;
}
int pop()
{
return stack[top--];
}
int main()
{
int n,a,sum=0;
scanf("%d",&n);
for(int i=1; i<=n; i++){
scanf("%d",&a);
if(a==0){
pop();
}
else{
push(a);
}
}
while(top!=-1){
sum+=pop();
}
printf("%d",sum);
return 0;
}
올바른괄호 체크하는법
1. ( 갯수와 ) 갯수가 같아야한다
2. 현재까지 나온 '(' 의 갯수 보다 ')' 갯수가 많아지는 순간 끝
( ( ( ) ) ) )
#include<stdio.h>
int top=-1;
char stack[50000]={};
void push()
{
if(top==50000) return ;
stack[++top];
}
char pop()
{
return stack[top--];
}
int main()
{
int a=0,b=0;
char str[50001]={};
scanf("%s",str);
for(int i=0; str[i]!=0; i++){
if(str[i]=='('){
push(str[i]);
}
else if(str[i]==')'){
if(top==-1){
printf("bad");
return 0;
}
pop();
}
}
if(top==-1){
printf("good");
}
else {
printf("bad");
}
return 0;
}
*/
#include <stdio.h>
#include <string.h>
int top=-1;
int stack[200]={};
void push(int n)
{
if(top==200) return;
stack[++top]=n;
}
char pop()
{
return stack[top--];
}
int main()
{
int n,num=0;
char str[200]={};
scanf("%d ",&n);
for(int i=1; i<=n; i++){
gets(str);
if(str[0]=='p'&&str[1]=='u'){
num=0;
for(int j=6; str[j]!=' '; j++){
num=10*num+str[j]-'0';
}
push(num);
}
else if(str[0]=='p' && str[1]=='o'){
if(top!=-1) pop();
}
else if(str[0]=='t'){
if(top==-1){
printf("-1\n");
}
else{
printf("%d\n",stack[top]);
}
}
else if(str[0]=='s'){
printf("%d\n",top+1);
}
else if(str[0]=='e'){
if(top==-1){
printf("true\n");
}
else{
printf("false\n");
}
}
}
return 0;
}