#include <stdio.h>
#include <stdlib.h>
int main()
{
int n=1,i,top;
int a[10001];
char ch[10];
while(n!=0)
{
scanf("%d",&n);
if(n<=0||n>10000)
continue;
top=0;
for(i=0;i<n;i++)
{
getchar();
scanf("%s",ch);
if(ch[0]=='P')
scanf("%d",&a[top++]);
else if(ch[0]=='O'&&top>0)
top--;
else if(ch[0]=='A')
{
if(top<=0) printf("E\n");
else printf("%d\n",a[top-1]);
}
}
printf("\n");
}
return 0;
}
这代码可以正常提交!
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n=1,i,top;
int a[10001];
char ch;
while(n!=0)
{
scanf("%d",&n);
if(n<=0||n>10000)
continue;
top=0;
for(i=0;i<n;i++)
{
getchar();
scanf("%c",&ch);
if(ch=='P')
scanf("%d",&a[top++]);
else if(ch=='O'&&top>0)
top--;
else if(ch=='A')
{
if(top<=0) printf("E\n");
else printf("%d\n",a[top-1]);
}
}
printf("\n");
}
return 0;
}这代码提交就会出现超时!到底为什么?
本文通过两个C语言程序示例对比分析了栈操作的效率问题。一个程序在提交时能够正常运行,而另一个则出现了超时的情况。通过对这两个程序的对比,文章探讨了导致效率差异的原因,特别是输入字符与字符串的不同处理方式对程序性能的影响。
200

被折叠的 条评论
为什么被折叠?



