有以下程序: #include int fun(int x) { int P; if(x==0‖x==1) return(3); P=x—fun(x一2); return p; }

admin2019-08-18  16

问题 有以下程序:
    #include
    int fun(int x)
    {
    int P;
    if(x==0‖x==1)
    return(3);
    P=x—fun(x一2);
    return p;
    }
    main()
    {
    prinff(“%d\n”,fun(7));
    }
    程序的运行结果是(    )。

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

答案A

解析 因为fun(int x)是一个递归函数,所以主函数中fun(7)经过3次递归调用,其过程可以描述为fun(7)=7一fun(5)=7一(5一fun(3)):7一(5一(3一fun(1)))=7一(5一(3—3))=7—5=2,所以最后的输出结果为2,因此A选项正确。
转载请注明原文地址:https://kaotiyun.com/show/eZRp777K
0

相关试题推荐
最新回复(0)