问题
单项选择题
以下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”的判定就不需要了。