yanzi

null

            上网时,偶然发现有人在变汉诺塔程序,于是就由C语言编了一个小小的程序。大家有兴趣可以see see !!
          我的汉诺塔程序采用的是递归调用的思想。
  汉诺问题的递归思想就是三步走:
原来都在A柱,辅助B柱,目标C
1)想办法把A柱上的除最底下一个以外的盘借助C柱全部移到 B柱,
2)把最后剩下的一个盘从A移到C
3 )把 B 柱上的盘想办法全部移到 C 柱。
 1 #include"stdio.h"
 2 void movedisk(int n,char fromneedle,char toneedle,char usingneedle); int i=0;
 3 main()
 4 {int m;
 5  int stop=1;
 6  printf("              **********************************\n");
 7  printf("              *   Welcome to my program!!!^-^  *\n");
 8  printf("              **********************************\n");
 9  printf("This is a Game called Hanoi,you can follow the steps below to begin!!!\n");
10   while(stop){i=0;
11               printf("Please input the number of the disk:");
12               scanf("%d",&m);
13               movedisk(m,'a','c','b');
14               printf("\nthe fewest steps you have to go is %d.\n",i);
15               printf("\nif you want to stop,input:0 ,else input:1\nInput:");
16               scanf("%d",&stop);
17              }
18    getch();
19  }
20 
21  void movedisk(int n,char fromneedle,char toneedle,char usingneedle)
22 {
23  if(n==1)
24     printf("[%d]  %c--->%c\t",++i,fromneedle,toneedle);
25   else if(n>1)
26      {movedisk(n-1,fromneedle,usingneedle,toneedle);
27       printf("[%d]  %c--->%c\t",++i,fromneedle,toneedle);
28       movedisk(n-1,usingneedle,toneedle,fromneedle);
29       }
30 
31   }

posted on 2006-05-19 20:49 yanzi0518 阅读(515) 评论(1)  编辑 收藏 引用

评论

# re: null 2006-05-21 09:30 flyingxu

能不能使用标题呢?让大家一目了然  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2006年5月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

导航

统计

常用链接

留言簿(1)

随笔档案

搜索

最新评论

  • 1. re: null
  • 能不能使用标题呢?让大家一目了然
  • --flyingxu