#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);
C++斐波那契数列两种递归实现及比较
于 2022-01-20 14:16:02 首次发布