黑马程序员技术交流社区

标题: 斐波那契数列 [打印本页]

作者: 夜神月No1    时间: 2015-11-21 22:43
标题: 斐波那契数列

/*
      1    1     2    3    5   8    13     21     34
       从第三项开始 后一个数等于前两个数之和。
      f = f(n-1) + f(n-2);
*/

#include <stdio.h>
#include <time.h>

/*
    求这个数列的第45项是多少,求和。
*/

//函数声明
long long int gui(int n);

//函数实现
long long int gui(int n)
{
    if(n < 3)
        return 1;
    return gui(n-1) + gui(n-2);
}
/*
     就是递归的效率低。
*/

int main(int argc, const char * argv[])
{
    clock_t start,end;
    double res = 0;
    start = clock();
    printf("%lld\n",gui(45));
    end = clock();
    //算用了多长时间
    res =(double)(end - start)/CLOCKS_PER_SEC;
    //输出
    printf("res = %.2lf 秒\n",res);
    return 0;
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2