c语言????

来源:百度知道 编辑:UC知道 时间:2024/05/29 19:36:08
(34)有以下程序
#include <stdio.h>
int f(int x)
{int y;
if(x==0||x==1) return (3);
y=x*x-f(x-2);
return y;
}
main()
{int z;
z=f(3); printf(“%d\n”,z);
}
程序的运行结果是
A)0 B)9 C)6 D)8
楼下的c 6
y=3*3-f(1)=6
f(1)=2 为什么????

选择c 这是递归算法,当f(3)时,
int f()中x=3;
if语句不成立,直接y=x*x-f(1); (1)
调用f()函数,
在调用中,x=1,满足if的(x==1),于是返回值为3,即(1)中的f(1)=3;
所以z=3*3-3=6

c 6
y=3*3-f(1)=6

问题补充:楼下的c 6
y=3*3-f(1)=6
f(1)=2 为什么????
f(1)=3 不是等于2 参见 if(x==0||x==1) return (3);

C 6
main()前面最好加上返回值类型,虽然C默认返回int