标签:iostream ati return src using iter 复杂 name main
#include <iostream>
#include <cmath>
using namespace std;
//2.斐波那契--递归版本
//fn=1;当n=0,1
//fn=fn-1+fn-2;当n>1
//算法复杂度O(2^n)
static int fibonacci(int n)
{
if (n<=1) return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}
//2.斐波那契--非递归版本
//1/sqrt(5) ( pow((1+sqrt(5))/2,n+1) - pow((1-sqrt(5))/2,n+1) )
//算法复杂度O(1)
static int fibonacci_iter(int n)
{
return 1 / sqrt(5) * (pow((1 + sqrt(5)) / 2, n + 1) - pow((1 - sqrt(5)) / 2, n + 1));
}
int main()
{
cout<<fibonacci(5)<<endl;
cout<<fibonacci_iter(5)<<endl;
cout << "hello world" << endl;
return 0;
}
标签:iostream ati return src using iter 复杂 name main
原文地址:https://www.cnblogs.com/tailiang/p/11712843.html