题解 | #【模板】栈#
【模板】栈
https://www.nowcoder.com/practice/104ce248c2f04cfb986b92d0548cccbf
#include <stdio.h>
#include <string.h>
int main() {
int a, b,m=0,top=-1,i=0,j=0,k=0,l;
scanf("%d", &m);
int s[m];
char c[15]={0};
for(i=0;i<m;i++){
//fgets(c, m, stdin);
scanf("%s",c);
l=sizeof(c);
b=0;
if(c[0]=='p'&&c[1]=='o'){
if(top==-1){
printf("error\n");
// for(j=0;j<15;j++){
// c[i]='\0';
// }
continue;
}
else{
printf("%d\n",s[top--]);
}
}
if(c[0]=='p'&&c[1]=='u'){
// for(j=5;j<l;j++){
// b=b*10+(c[j]-'0');
// }
scanf("%d",&b);
s[++top]=b;
// for(j=0;j<15;j++){
// c[i]='\0';
// }
continue;
}
if (c[0]=='t') {
if(top==-1){
printf("error\n");
}
else{
printf("%d\n",s[top]);
}
// for(j=0;j<15;j++){
// c[i]='\0';
// }
continue;
}
}
return 0;
}

查看2道真题和解析