#include<iostream.h>
#include<stdio.h>
#include <conio.h>
#define MAXSTEPS 10
unsigned long int xianxing(int steps, int once)//线性计算方法,用一个数组保存结果
//计算完毕后,返回数组最后一个值,就是结果。
{ unsigned long int save[MAXSTEPS + 1];
int i, j;
unsigned long int temp;
for(i = 0; i <= MAXSTEPS; i++)
save[i] = 0;
if(steps > MAXSTEPS || steps <= 0)
return 0;
save[0] = 1;
save[1] = 1;
if(steps == 1)
return save[0];
int min = steps <= once ? steps : once;
for(i = 2; i <= min; i++)
{
for(j = 1; j <= i; j++)
{
save[i] += save[i - j];
}
}
if(steps > once)
{
for(i = once + 1; i <= steps; i++)
{
for(j = 1; j <= once; j++)
{
temp = save[i];
save[i] += save[i - j];
if(save[i] <= temp)//表示计算溢出!
return 0;
}
}
}
return save[steps];
}
void main()
{
int upStairs,steps,s;
char a;
do
{
cout<<"请输入阶梯数及步长:"<<endl;
cin>>upStairs>>steps;
s=xianxing(upStairs,steps);
cout<<"总共的走法为:"<<s<<"种"<<endl;
a=_getch();
}
while(a!=' ');
}
十级台阶走法(非递归)
最新推荐文章于 2021-03-16 09:59:47 发布