下列程序的输出结果为 #include<iostream.h> int func(int n) { if(n<1) return 1; else retur n+func(n-1) ; return

admin2010-12-16  22

问题 下列程序的输出结果为
   #include<iostream.h>
   int func(int n)
   {
        if(n<1) return 1;
   else retur n+func(n-1) ;
        return 0;
   }
   void main( )
   {
        cout < < func(5) < < endl;
   }

选项 A、0
B、10
C、15
D、16

答案D

解析 本题考查的是递归函数的使用,题中递归函数的结束条件为n<1,递推公式为f(n) =n +f(n-1) ,所以计算结果为5+4+3+2+1+1,最后一个1为n=0时的返回值。
转载请注明原文地址:https://jikaoti.com/ti/bqL0FFFM
0

最新回复(0)