九度OJ-1108-堆棧的使用

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int n;
char str[10];
int top;
int stack[10010];

int main(){
    freopen("input.txt","r",stdin);

    while(scanf("%d",&n)!=EOF&&n){
        top=-1;
        memset(stack,0,sizeof(stack));
        for (int i = 0; i < n; ++i)
        {
            scanf("%s",str);
            if (str[0]=='A')
            {
                if (top>=0)
                    printf("%d\n",stack[top]);
                else
                    printf("E\n");
            }else if(str[0]=='P'){
                int tmp;
                scanf("%d",&tmp);
                stack[++top]=tmp;
            }else
                if (top>=0)
                    top--;          
        }
        printf("\n");
    }
    return 0;
}
發佈了45 篇原創文章 · 獲贊 8 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章