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

admin2020-06-16  27

问题 有以下程序:
    #include
    int 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、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/AiCp777K
0

最新回复(0)