美文网首页
斐波那契数列 递归算法(执行时间)

斐波那契数列 递归算法(执行时间)

作者: 壹顾倾城 | 来源:发表于2020-12-06 10:37 被阅读0次
/********************************
 * 程序名称:斐波那契数列 递归算法 
 * 作    者:tiaya@qq.com
 * 开发时间: 年  月  日
 *******************************/
#include <iostream>
#include <cstdio>
#include <ctime>
using namespace std;

//int fab(int n)
double fab(int n) {
    if (n<3) {
        return 1;
        cout << "n=" << n << endl;
    } else {
        cout << "n=" << n << endl;
        return fab(n-1) + fab(n-2);
    }
} 
//main() star
int main() {
    //code here
    clock_t start,finish;
    double totaltime;
    start=clock();
    
    int n;
    cin >> n;   
    cout << "第" << n <<"项是:" << fab(n) << endl;
    
    finish=clock();
    totaltime=(double)(finish-start)/CLOCKS_PER_SEC;
    cout<<"\n此程序的运行时间为"<<totaltime<<"秒!"<<endl;
    
    return 0;
}

测试:
输入数据:30

30

输出数据:

n=6
n=5
n=4
n=3
n=3
n=4
n=3
n=5
n=4
n=3
n=3
n=6
n=5
n=4
n=3
n=3
n=4
n=3
第30项是:832040
此程序的运行时间为45.016秒!
--------------------------------
Process exited after 44.44 seconds with return value 0
请按任意键继续. . .

相关文章

网友评论

      本文标题:斐波那契数列 递归算法(执行时间)

      本文链接:https://www.haomeiwen.com/subject/bbhowktx.html