有以下程序 fun(int x) main() {int p; { if(x==0}‖x==1)return(3);printf("%d\n",fun(7)); p=x-fun(x-2); } re

admin2020-07-28  15

问题 有以下程序
    fun(int x)    main()
    {int p;    {
    if(x==0}‖x==1)return(3);printf("%d\n",fun(7));
    p=x-fun(x-2);    }
    return p;}
    执行后的输出结果是

选项 A、3
B、7
C、0
D、2

答案D

解析 因为在函数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。
转载请注明原文地址:https://kaotiyun.com/show/j93p777K
0

最新回复(0)