Climbing Stairs

来源:互联网 发布:四川省网络试听作品 编辑:程序博客网 时间:2024/06/03 23:42

You are climbing a stair case. It takes n steps to reach to the top.

Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?

该题的难道是模型的建立:利用数学归纳法:

n=1, f(n)=1;

n=2,f(n)=2;

n=k(>=2), f(k)=f(k-2)+f(k-1);

#include<iostream>
using namespace std;

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


int main()
{
int k,res;
cin >> k ;
res=climbStairs(k);
cout<< res <<endl;
return 1;


}

0 0
原创粉丝点击