| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:求教。为什么会超时?问题已解决数组定义小了。重新附上代码。In Reply To:求教。为什么会超时? Posted by:2326564155 at 2014-08-15 19:54:30 > #include<cstdio>
> int a[1000],n;
>
> int ok()
> {
> int i,top=0,k=1;
> int stack[1000];
> for(i=1;i<=n;i++)
> {
> stack[++top]=i;
> while(top&&stack[top]==a[k])
> {
> top--;
> k++;
> }
> }
> if(top) return 0;
> else return 1;
> }
>
> int main()
> {
> int f=0,i;
> while(scanf("%d",&n),n)
> {
> if(f)
> printf("\n");
> f=1;
> while(scanf("%d",&a[1]),a[1])
> {
> for(i=2;i<=n;i++)
> scanf("%d",&a[i]);
> if(ok())
> printf("Yes\n");
> else printf("No\n");
> }
> }
> return 0;
> }
>
>
更改过的,AC
#include<cstdio>
int a[1010],n;
int ok()
{
int i,top=0,k=1;
int b[1010];
memset(b,0,sizeof(b));
for(i=1;i<=n;i++)
{
b[++top]=i;
while(top&&b[top]==a[k])
{
top--;
k++;
}
}
if(top) return 0;
else return 1;
}
int main()
{
int f=0,i;
while(scanf("%d",&n),n)
{
if(f)
printf("\n");
f=1;
while(scanf("%d",&a[1]),a[1])
{
for(i=2;i<=n;i++)
scanf("%d",&a[i]);
if(ok())
printf("Yes\n");
else printf("No\n");
}
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator