十级台阶,一次跨一步,二步,有多少种走法?
// stairs.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
/*
//递归
int step(int n)
{
if (n<=0) return 0;
if (n==1) return 1;
if (n==2) return 2;
if (n>2) return step(n-1)+step(n-2);
}
*/
int combination(int n, int m)
{
int prod=1;
for (int i=n;i>n-m;i--)
{
prod*=i;
}
for(int i=1;i<=m;i++)
{
prod/=i;
}
return prod;
}
//非递归
int step(int n)
{
int m=0; //二的个数;
int twos=0;//二的和
int sum=0;
while(twos<=n)
{
sum=sum+combination(n-m,m);
m++;
twos=m*2;
}
return sum;
}
int _tmain(int argc, _TCHAR* argv[])
{
int n=10;
//cout<<"c(5,3)="<<combination(3,3)<<endl;
//cout<<"c(5,0)="<<combination(3,0)<<endl;
cout<<n<<"阶梯的走法为:"<<step(10)<<endl;
system("pause");
return 0;
}