AcWing 828. 模拟栈

时间:2021-01-11 11:07:55   收藏:0   阅读:0

实现一个栈,栈初始为空,支持四种操作:

(1) “push x” – 向栈顶插入一个数x;

(2) “pop” – 从栈顶弹出一个数;

(3) “empty” – 判断栈是否为空;

(4) “query” – 查询栈顶元素。

现在要对栈进行M个操作,其中的每个操作3和操作4都要

#include<bits/stdc++.h>
#define N 100010
using namespace std;
int n;
int a[N],top;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        string b;int k;
        cin>>b;
        if(b=="empty")if(top==0)puts("YES");else puts("NO");
        if(b=="push"){scanf("%d",&k);a[++top]=k;}
        if(b=="query")printf("%d\n",a[top]);
        if(b=="pop"&&top)top--;

    }
    return 0;
}

 

输出相应的结果。

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!