某人上楼梯,他一步可以迈一个台阶,两个台阶或三个台阶,共有n个台阶,编程输出他所有可能上法.
如:有4个台阶,输出应是:
1 1 1 1
1 1 2
1 2 1
1 3
2 1 1
2 2
3 1
/* stair.c
The problem of stair
Copyright By Jimmy, 2002.11
All Rights Reserved.
*/
#define STAIR_NUM 5
int total=0;
int index;
int que[STAIR_NUM];
void outputstep()
{
int i;
for(i=0;i<index;i++)
printf("-%d",que[i]);
printf("-\n");
}
void step(int n)
{
if (n==0)
{
total++;
printf("---------the NO.%d ----------\n",total);
outputstep();
return ;
}
que[index++]=1;
step(n-1);
--index;
if (n>1)
{
que[index++]=2;
step(n-2);
--index;
}
if(n>2)
{
que[index++]=3;
step(n-3);
--index;
}
}
main()
{
printf("\n");
printf("--------------------------------------\n");
printf(" stair step \n");
printf("--------------------------------------\n");
step(STAIR_NUM);
printf("\n the total is %d \n",total);
}
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |