标签:
斐波那契数列
《剑指Offer》P73
可用以实现青蛙跳台阶问题,线段问题等
<span style="font-size:12px;">//较差的实现方式一,里面包涵了很多重复计算
long long Fibonacci::Fibonacci_fun1(int n)
{
if (n <= 0)
{
return 0;
}
if (n == 1)
{
return 1;
}
return Fibonacci_fun1(n - 1) + Fibonacci_fun1(n - 2);
}
//直接使用循环来实现
long long Fibonacci::Fibonacci_fun2(int n)
{
//不能忽略n为1,2的情况
if (n < 0)
{
return 0;
}
int temp[] = {0,1};
if (n < 2)
{
return temp[n];
}
long long temp1 = temp[0];
long long temp2 = temp[1];
long long result = 0;
for (int i = 2; i <= n;i ++)
{
result = temp2 + temp1;
temp1 = temp2;
temp2 = result;
}
return result;
}
</span>标签:
原文地址:http://blog.csdn.net/woliuyunyicai/article/details/44177591