台阶问题 某人上楼梯,他一步可以迈一个台阶,两个台阶或三个台阶,共有n个台阶,编程输出他所有可能上法.如:有4个台阶,输出应是:1 1 1 11 1 21 2 11 32 1 12 23 1 /* stair.c The problem of stair Copyright By Jimmy, 2002.11 All Rights Reserved.*/ #define STAIR_NUM 5int 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);}

评论