楼梯问题:一次最多跨两个阶梯,有多少种走法
采用排列组合的方法在C语言下实现。前24层结果和公布的是一致的,但25层以后出现分歧了,调试几次后没发现问题,测试环境下的int是32位的。疑惑,还请高人指点,显示结果如下:
NO20:10946
NO21:17711
NO22:28657
NO23:46368
NO24:73735
NO25:105676
NO26:162973
NO27:140125
NO28:254929
NO29:322365
NO30:449574
NO31:568275
NO32:-45160
NO33:249710
NO34:523678
NO35:969095
=======================源代码===============
#include<stdio.h>
#define N 50
int stage(int stages, int spansteps)
{
int router = 0;
int i = 0;
for(i=0;i<=stages/spansteps;i++)
{
router += Combination(stages-i,i);
}
return router;
}
// C(Combination ) in Probability theory and mathematical statistics
int Combination (int n, int r)
{
return (int)Arrangment(n,r)/Arrangment(r,r);
}
//A(Arrangment ) in Probability theory and mathematical statistics
int Arrangment(int n, int r)
{
int product = 1;
for(r;r>0;r--)
{
product *=n;
n--;
}
return (int)product;
}
int Power(int base, int power)
{
int result = 1;
do
{
result *=base;
power -- ;
}
while(1!=power);
return result;
}
int main(void)
{
int i = 20;
while(i<=35)
{
printf("NO%d:%d\n",i,stage(i,2));
i++;
}
printf("%d\n",Power(2,31));
return 0;
}