B3614题解
_PerfectYouth_ · · 题解
思路
题目大意是让我们建立一个stack来进行模拟按照题目要求来输入输出,大概思考一下,如果我们用stack来做题的话属实有点复杂,按照题目大意写出一轮循环之后都要把stack清空,不如用数组来的快。最后观察数据规模,x<2的64次方,那么我们必须使用unsigned long long。
解题步骤
1.运用while不断循环输入。
2.判断字符串是push,pop,query还是size,当字符串为push时再输入一个即将入栈的数 x 。
3.按照题目模拟 + 输出即可。
具体代码如下:
#include<bits/stdc++.h>//万能头
using namespace std;
unsigned long long s[10000006],top,x;//定义直接使用unsigned long long,数组可以定义大一些
int main(){
int t,n;//定义t和n
cin>>t;
while(t>0){//while循环
t--;
cin>>n;//输入
top=0;//将栈顶初始化为1
while(n>0){//while循环
n--;
string o;
cin>>o;//定义输入字符串o
if(o=="push"){
cin>>x;//将x压入栈
s[++top]=x;
}
if(o=="pop"){
if(top<1)cout<<"Empty"<<endl;//判断栈是否为空
else top--;不为空则--top
}
if(o=="query"){
if(top<1)cout<<"Anguei!"<<endl;//老样地判断栈是否为空
else cout<<s[top]<<endl;//否则输出栈顶
}
if(o=="size"){
cout<<top<<endl;//输出栈内元素个数
}
}
}
return 0;//养成一个好习惯
}