问题
单项选择题
以下程序运行后的输出结果是
#include<stdio.h>
int f(int t[], int n);
main()
int a[4]=1, 2, 3, 4, s;
s=f(a, 4); printf("%d\n", s);
int f(int t[], int n)
if(n>0) return t[n-1]+f(t, n-1);
else return 0;
A) 4
B) 10
C) 14
D) 6
答案
参考答案:B
解析: 本题考查函数递归调用。被调函数f中的t数组与main函数中的a数组共用一组存储单元,根据函数递归调用流程可知:s=f(a, 4)=t[3]+f(t,3),f(t, 3)=t[2]+f(t, 2),f(t, 2)=t[1]+f(t, 1),f(t, 1)=t[0]+f(t, 0),f(t, 0)=0;将f(t, 0)=0代入上述公式进行递推:f(t, 1)=t[0]+f(t, 0)=1+0=1,f(t, 2)=t[1]+f(t, 1)=2+1=3,f(t, 3)=t[2]+f(t, 2)=3+3=6,s=f(a, 4)=t[3]+f(t, 3)=4+6=10。