阶梯问题

来源:互联网 发布:uc2监控软件 编辑:程序博客网 时间:2024/04/28 05:19

有一个楼梯,100级台阶,然后,你从最下面开始往上走。你的任意一步,可以跨2级台阶,也可以跨1级台阶。请问,从地面到第100级台阶,有多少种走法?

 

#include "iostream"
#include "time.h"
using namespace std;


int Total_ways(int n)
{
 if(n<=2)
  return n;
 else
  return Total_ways(n-1)+Total_ways(n-2);
}


int main()
{
 int n,way;
 clock_t start;
 cout<<"Please input the number of steps:";
 cin>>n;

 start=clock();
    way=Total_ways(n);
 cout<<n<<" stairs have "<<way<<" ways to go!"<<endl;
 cout<<"running time is:"<<clock()-start<<endl;

  return 0;
}

原创粉丝点击