以下C程序段的输出结果是(35)。    #include<stdio.h)long fun(int n){    long s;         if(n==1 || n==2)     s=2;         else             s=n

admin2009-02-15  23

问题 以下C程序段的输出结果是(35)。    #include<stdio.h)long fun(int n){    long s;         if(n==1 || n==2)     s=2;         else             s=n+fun(n-1);         return s;    }    void main(){    printf("\n%1d",fun(4));  )

选项 A、5
B、7
C、9
D、11

答案C

解析 本试题考查函数的递归调用。程序在n=1或n=2时是出口条件,不再递归,否则一直执行s=n+fun(n-1)的操作。展开此求和公式,有s=n+fun(3)=4+3+fun(2)=4+3+2=9。另外,如果调用函数fun的实参≥2,则出口条件“n==1”的判定就不需要了。
转载请注明原文地址:https://kaotiyun.com/show/nyjZ777K
0

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