今天考二级,被绊脚了,请教一下
今天考二级,被绊脚了,请教一下:
int fun(int t)
{ int s;
if(t==0||t==1) return t;
s=fun(t-1)+fun(t-2);
return s;
}
main()
{ printf("%d\n",fun(1000));
}
这个程序运行时为何总是死机???
今天考二级,被绊脚了,请教一下:
int fun(int t)
{ int s;
if(t==0||t==1) return t;
s=fun(t-1)+fun(t-2);
return s;
}
main()
{ printf("%d\n",fun(1000));
}
这个程序运行时为何总是死机???
是,都怨那道题,给了那么大一个数,结果总是死机。这是一递归函数中很有名一道题:斐波拉契级数.
声名:题中当t为0和1时应返回1,而非t.可能是我上机时看错题了
12楼的大哥确实厉害,运算速度神快,顶一下,供大家学习
不过我觉得有个地方应该改一下,这样t为0和1时都能输出1,不知对否
int fun(int t)
{
int a[3] = {1,1,2};
int s=0;
int i;
for(i=0;i<t;i++)
{
a[s] = a[(s+2) % 3] + a[(s+1) % 3];
s = (s+1) % 3;
}
return a[s];
}