斐波那契数列的几种不同的算法,递归的不同实现:
1 #include "stdio.h" 2 #include "math.h" 3 4 5 int factorial_tail(int n,int acc1,int acc2) 6 { 7 if (n < 2) 8 return acc1; 9 else 10 return factorial_tail(n-1,acc2,acc1+acc2); 11 } 12 13 int factorial_head(int n) 14 { 15 if(n<=2) 16 return 1; 17 else 18 return factorial_head(n-1)+factorial_head(n-2); 19 } 20 21 int main(void) 22 { 23 int i, n, rs; 24 25 printf("请输入斐波那契数n:"); 26 scanf("%d",&n); 27 28 rs = factorial_tail(n, 1, 1); 29 //rs=factorial_head(n); 30 printf("%d ", rs); 31 32 return 0; 33 }
原文:http://www.cnblogs.com/zzsf/p/3825281.html