fork(1) download
  1. #include <stdio.h>
  2. long Fib(int a);
  3. long int count;
  4. int main(void)
  5. {
  6. int n, i, x;
  7. printf("Input n:\n");
  8. scanf("%d", &n);
  9. for (i=1; i<=n; i++)
  10. {
  11. count=0;
  12. x = Fib(i);
  13. printf("Fib(%d)=%d,count=%d\n",i,x,count);
  14. }
  15. return 0;
  16. }
  17. /* 函数功能:用递归法计算Fibonacci数列中的第n项的值 */
  18. long Fib(int n)
  19. {
  20. count++;
  21. if (n == 1) return 1; /* 基线情况 */
  22. else if (n == 2) return 1; /* 基线情况 */
  23. else return (Fib(n-1) + Fib(n-2)); /* 一般情况 */
  24.  
  25. }
Success #stdin #stdout 0s 544KB
stdin
2
stdout
Input n:
Fib(1)=1,count=1
Fib(2)=1,count=1