📜  算法|递归问题9

📅  最后修改于: 2021-06-29 19:02:50             🧑  作者: Mango

预测以下程序的输出

#include 
  
int fun(int n)
{
    if (n == 4)
       return n;
    else return 2*fun(n+1);
}
  
  
int main()
{
   printf("%d ", fun(2));
   return 0;
}

(A) 4
(B) 8
(C) 16
(D)运行时错误答案: (C)
解释:

Fun(2) = 2 * Fun(3) and Fun(3) = 2 * Fun(4) ....(i)
Fun(4) = 4 ......(ii)
From equation (i) and (ii),
Fun(2) = 2 * 2 * Fun(4)
Fun(2) = 2 * 2 * 4
Fun(2) = 16. 

So, C is the correct answer

这个问题的测验