标签:函数 turn include cout str new int names 斐波那契
int f1(int n) {
if(n < 1) {
return 0;
}else if(n == 1 || n == 2) {
return 1;
}
return f1(n-1) + f1(n-2);
}
#include <iostream>
using namespace std;
long long Fibonacci(unsigned n)
{
int result[2] = {0,1};
if(n<2) return result[n];
long long fiboOne = 1;
long long fiboTwo = 0;
long fiboNew = 0; //最后结果
for(unsigned int i=2; i<=n; ++i)
{
fiboNew = fiboOne + fiboTwo;
fiboTwo = fiboOne;
fiboOne = fiboNew;
}
return fiboNew;
}
int main() {
long long n;
cin>>n;
cout << Fibonacci(n)<<endl;
return 0;
}
标签:函数 turn include cout str new int names 斐波那契
原文地址:https://www.cnblogs.com/52yu/p/13886581.html