有以下程序 fun(int x) { int p; if(x==0‖x==1)return(3); p=x-fun(x-2); return p; } main() { pri

admin2019-12-17  21

问题 有以下程序
    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、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/t2Cp777K
0

最新回复(0)