#include<iostream>
#include<chrono>
#include<thread>
using namespace std;
using namespace chrono;
//递归一
int fibonacci_01(int n) {
if (n <= 0) {
return 0;
}
if (n == 1) {
return 1;
}
return fibonacci_01(n - 1) + fibonacci_01(n - 2);
}
//递归二
int fibonacci_02(int first, int second, int n) {
if (n <= 0) {
return 0;
}
else if (n < 2) {
return 1;
}
else if (n == 2) {
return first + second;
}
else {
return fibonacci_02(second, second + first, n - 1);
}
}
//递归一所用时间
void time_consumption01() {
int n ;
cout << "请输入n的值:" << endl;
cin >> n;
//运算开始的时刻
milliseconds start_time = duration_cast<milliseconds>(
system_clock::now().time_since_epoch()
);
fibonacci_01(n);
//运算结束的时刻
milliseconds end_time = duration_cast<milliseconds>(
system_clock::now().time_since_epoch()
);
int num = fibonacci_01(n);
cout << "运行时间为:" << milliseconds(end_time).count(
C++斐波那契数列两种递归实现及比较
于 2022-01-20 14:16:02 首次发布
本文详细介绍了C++中斐波那契数列的两种递归实现方式,并对它们的效率进行了比较。通过实例代码展示,深入理解递归在解决斐波那契问题中的应用。

最低0.47元/天 解锁文章
2951

被折叠的 条评论
为什么被折叠?



