1. 递归
f(n) = f(n-1) + f(n-2)
2. 从下向上计算
long long Fibonacci(int n)
{
int result[2] = { 0, 1 };
if (n < 2)
{
return result[n];
}
long long fibNminusOne = 1;
long long fibNminusTwo = 0;
long long fibN = 0;
for (unsigned int i = 2; i <= n; i++)
{
fibN = fibNminusOne + fibNminusTwo;
fibNminusTwo = fibNminusOne;
fibNminusOne = fibN;
}
return fibN;
}
3. 数学公式
时间: 2024-12-31 06:50:18