Description
有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法?
Input
输入数据首先包含一个整数N,表示测试实例的个数,然后是N行数据,每行包含一个整数M(1<=M<=40),表示楼梯的级数。
Output
对于每个测试实例,请输出不同走法的数量
Sample Input
2
2
3
Sample Output
1
2
思路
阶乘
全排列中含有重复元素的情况
AC代码
#include <stdio.h>
#include <stdlib.h>
typedef long long LL;
int max( int a, int b )
{
return a > b ? a : b;
}
int min( int a, int b )
{
return a < b ? a : b;
}
LL JC( int sum, int a, int b )
{
LL fz = sum, fm = min(a,b);
if( fm == 0 )
return 1;
for( int i = sum - 1; i > max(a,b); i-- )
fz *= i;
for( int i = min(a,b)-1; i > 0; i-- )
fm *= i;
LL re = fz/fm;
return re;
}
int main()
{
int T, floor;
scanf("%d",&T);
while(T--)
{
LL result = 0;
scanf("%d",&floor);
if( floor == 1 ){
puts("0");
continue;
}
int n = floor - 1;
for( int two = 0; two <= (floor-1)/2 ; two++ )
{
int one = n - 2*two;
LL re = JC(one+two,one,two);
result += re;
}
printf("%lld
",result);
}
return 0;
}