正文

台阶问题 2007-05-02 21:59:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/andyhou/25486.html

分享到:

台阶问题     某人上楼梯,他一步可以迈一个台阶,两个台阶或三个台阶,共有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);}   

阅读(2793) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册