一道C语言题,# include int fun (int n,int *s){int f1,f2;if (n==1||n==2) *s=1;else {fun(n-1,&f1); /* fun(5,&f1) fun(4,&f1) fun(3,&f1) fun(2,&f1) */fun(n-2,&f2); /* fun(4,&f2) fun(2,&f2) */*s=f1+f2;}}main(){int x;fun(6,&x);printf ("\n%d\n",x);}输

来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/06 04:46:41
一道C语言题,#includeintfun(intn,int*s){intf1,f2;if(n==1||n==2)*s=1;else{fun(n-1,&f1);/*fun(5,&f1)fun(4,&f

一道C语言题,# include int fun (int n,int *s){int f1,f2;if (n==1||n==2) *s=1;else {fun(n-1,&f1); /* fun(5,&f1) fun(4,&f1) fun(3,&f1) fun(2,&f1) */fun(n-2,&f2); /* fun(4,&f2) fun(2,&f2) */*s=f1+f2;}}main(){int x;fun(6,&x);printf ("\n%d\n",x);}输
一道C语言题,
# include
int fun (int n,int *s)
{
int f1,f2;
if (n==1||n==2) *s=1;
else
{
fun(n-1,&f1); /* fun(5,&f1) fun(4,&f1) fun(3,&f1) fun(2,&f1) */
fun(n-2,&f2); /* fun(4,&f2) fun(2,&f2) */
*s=f1+f2;
}
}
main()
{
int x;
fun(6,&x);
printf ("\n%d\n",x);
}
输出结果为什么是8?

一道C语言题,# include int fun (int n,int *s){int f1,f2;if (n==1||n==2) *s=1;else {fun(n-1,&f1); /* fun(5,&f1) fun(4,&f1) fun(3,&f1) fun(2,&f1) */fun(n-2,&f2); /* fun(4,&f2) fun(2,&f2) */*s=f1+f2;}}main(){int x;fun(6,&x);printf ("\n%d\n",x);}输
结果不应该是8吗?
x = fun(6) = fun(5) + fun(4) = (fun(4) + fun(3) )+( fun(3) + fun(2)) = ((fun(3) + fun(2)) +( fun(2) + fun(1))
+ (fun(2) + fun(1)) + 1 = fun(2) + fun(1) + 6 = 8