首页 > 代码库 > 剑指Offer07 斐波那契数列

剑指Offer07 斐波那契数列

 1 /************************************************************************* 2     > File Name: 07_Fibonacci.c 3     > Author: Juntaran 4     > Mail: JuntaranMail@gmail.com 5     > Created Time: 2016年08月29日 星期一 20时23分54秒 6  ************************************************************************/ 7  8 #include <stdio.h> 9 10 long long Fibonacci1(int n)11 {12     if (n <= 0)13         return 0;14     if (n == 1)15         return 1;16     return Fibonacci1(n-2) + Fibonacci1(n-1);17 }18 19 long long Fibonacci2(int n)20 {21     int result[2] = {0, 1};22     if (n < 2)23         return result[n];24     25     long long fibNum1 = 0;26     long long fibNum2 = 1;27     long long temp = 0;28     29     for (int i = 2; i <= n; ++i)30     {31         temp = fibNum1 + fibNum2;32         fibNum1 = fibNum2;33         fibNum2 = temp;34     }35     return temp;36 }37 38 int main()39 {40     int n = 100;41     long long ret1, ret2;42     ret1 = Fibonacci1(n);43     ret2 = Fibonacci2(n);44     printf("ret1 is %lld, ret2 is %lld\n", ret1, ret2);45 }

 

剑指Offer07 斐波那契数列