ted 2015-03-13 10:42:36
点我顶贴
收藏
删除
尼玛我找了半天的错,最后几乎改成和讨论区那个"C语言 题解"一样了还是过不了第6点(输出为0)
如下代码是90分,为何那个讨论中的代码数组范围一样有100分??以下代码数组改大至300也能100分。有大神深入分析一下原因么。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
long i,n,now,count;
char str[255];
long next[201]={0};
char arrived[201]={0};
scanf("%ld",&n);
for (i=1;i<=n;i++)
{
scanf("%s",str);
if(str[0]=='j'&&str[1]=='m'&&str[2]=='p'&&str[3]==0)
scanf("%ld",&next[i]);
gets(str);
}
now=1;
count=0;
while (now<=n)
{
count++;
arrived[now]=1;
if (next[now]==0)
now++;
else
now=next[now];
if (arrived[now])
{
printf("0");
return 0;
}
}
printf("%ld",count);
return 0;
}
#2 ted@2015-03-13 10:57:54
33209
回复
删除
终于懂了,是if(arrived[now]){}这句要放在循环开头,不然此时now已经大于n了....我之前竟然没发现,太困了吧..