您的位置:首页 > 职场人生

【算法题】:跳台阶问题及斐波那契Fibonacci序列

2013-07-31 20:49 281 查看
题目:一个台阶总共有n级,如果一次可以跳1级,也可以跳2级。求总共有多少总跳法,并分析算法的时间复杂度。

分析:如果只有1级台阶,那显然只有一种跳法。如果有2级台阶,那就有两种跳的方法了:一种是分两次跳,每次跳1级;另外一种就是一次跳2级。

现在我们再来讨论一般情况。我们把n级台阶时的跳法看成是n的函数,记为f(n)。当n>2时,第一次跳的时候就有两种不同的选择:一是第一次只跳1级,此时跳法数目等于后面剩下的n-1级台阶的跳法数目,即为f(n-1);另外一种选择是第一次跳2级,此时跳法数目等于后面剩下的n-2级台阶的跳法数目,即为f(n-2)。因此n级台阶时的不同跳法的总数f(n)=f(n-1)+(f-2)。

我们把上面的分析用一个公式总结如下:
f(1)=1

f(2)= 2

f(n)=f(n-1)+(f-2)   n>2

分析到这里,相信很多人都能看出这就是我们熟悉的Fibonacci序列。

点击打开:斐波那契数列的递归求解与非递归求解

扩展: 当跳台阶的选择多了呢?比如说 每次可以跳3个台阶;按照同样的方法分析,如下公式:

f(1)=1

f(2)= 2

f(3)=4   //    1+1+1       1+2       2+1      3

f(n)=f(n-1)+(f-2)+f(n-3)   n>3

继续拓展:一个台阶总共有n级,如果一次可以跳1级,也可以跳2级......它也可以跳上n级。此时该青蛙跳上一个n级的台阶总共有多少种跳法?

分析:用Fib(n)表示跳上n阶台阶的跳法数,一次性跳上n阶台阶的跳法数1(n阶跳),设定Fib(0) = 1;

当n = 1 时, 只有一种跳法,即1阶跳:Fib(1) = 1;

当n = 2 时, 有两种跳的方式,一阶跳和二阶跳:Fib(2) = Fib(1) + Fib(0) = 2;

当n = 3 时,有三种跳的方式,第一次跳出一阶后,后面还有Fib(3-1)中跳法; 第一次跳出二阶后,后面还有Fib(3-2)中跳法;第一次跳出三阶后,后面还有Fib(3-3)中跳法

Fib(3) = Fib(2) + Fib(1)+Fib(0)=4;

当n = n 时,共有n种跳的方式,第一次跳出一阶后,后面还有Fib(n-1)中跳法; 第一次跳出二阶后,后面还有Fib(n-2)中跳法..........................第一次跳出n阶后,后面还有

Fib(n-n)中跳法.

Fib(n) = Fib(n-1)+Fib(n-2)+Fib(n-3)+..........+Fib(n-n)=Fib(0)+Fib(1)+Fib(2)+.......+Fib(n-1)

又因为Fib(n-1)=Fib(0)+Fib(1)+Fib(2)+.......+Fib(n-2)

两式相减得:Fib(n)-Fib(n-1)=Fib(n-1) =====》 Fib(n) = 2*Fib(n-1) n >= 2  这是一个公比为2的等比数列

#include<cstdio>
#include<iostream>
#include<time.h>
using namespace std;
int Funct( int n )
{
if(n==0) return 1;
if(n==1) return 1;
return Funct(n-1) + Funct(n-2);
}
int Foo( int n ) // n 为非负整数
{
int first=1;//应该初始化a为1而不是0
int second=1;
int first_plus_second;
if(n==0) first_plus_second=1;
else if(n==1) first_plus_second=1;
else for(int i=2;i<=n;i++) //应该n从2开始算起
{
first_plus_second=first+second;
first=second;
second=first_plus_second;
}
return first_plus_second;
}
void testRunningTime()
{
int n;
long now=0;
cout<<"请输入:\n";
while(cin>>n)
{
now=clock();
cout<<"----   Foo("<<n<<")= "<<Foo(n)<<endl;
cout<<"----   Foo("<<n<<") running time is :"
<<(double)(clock()-now)/CLOCKS_PER_SEC<<endl;
now=clock();
cout<<"----   Funct("<<n<<")= "<<Funct(n)<<endl;
cout<<"----   Funct("<<n<<") running time is :"
<<(double)(clock()-now)/CLOCKS_PER_SEC<<endl;
cout<<"请继续输入:\n";
}
}
int main()
{
testRunningTime();
}
/**************************************************
程序运行结果:
请输入:
1
----   Foo(1)= 1
----   Foo(1) running time is :0
----   Funct(1)= 1
----   Funct(1) running time is :0
请继续输入:
2
----   Foo(2)= 2
----   Foo(2) running time is :0.015
----   Funct(2)= 2
----   Funct(2) running time is :0
请继续输入:
22
----   Foo(22)= 28657
----   Foo(22) running time is :0
----   Funct(22)= 28657
----   Funct(22) running time is :0.015
请继续输入:
33
----   Foo(33)= 5702887
----   Foo(33) running time is :0
----   Funct(33)= 5702887
----   Funct(33) running time is :0.078
请继续输入:
40
----   Foo(40)= 165580141
----   Foo(40) running time is :0
----   Funct(40)= 165580141
----   Funct(40) running time is :1.653
请继续输入:
45
----   Foo(45)= 1836311903
----   Foo(45) running time is :0
----   Funct(45)= 1836311903
----   Funct(45) running time is :18.658
请继续输入:
^Z

Process returned 0 (0x0)   execution time : 70.512 s
Press any key to continue.

***************************************************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 笔试面试 递归