问题 单项选择题

有下列程序:
fun(int x)
int p;
if(x==0 ||x==1) return(3);
p=x-fun(x-2);
return p;main()
printf("%d\n",fun(7));
执行后的输出结果是( )。

A.7

B.3

C.2

D.0

答案

参考答案:C

解析: 因为在函数fun(int x)中,如果参数x等于0或1时,返回值3。否则p=x-fun(x-2)这是一个递归函数,所以在主函数调用fun(7)时,其过程为:
"fun(7)=7-fun(5)=7-(5-fun(3))-7-(5-(3-fun(1)))=7-(5-(3-3))=7-5=2",所以最后的输出结果为2。

问答题 简答题
判断题