yanzi

2006年5月19日

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 @ 2006-05-19 20:49 yanzi0518 阅读(517) | 评论 (1)编辑 收藏

仅列出标题  
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

留言簿(1)

随笔档案

搜索

最新评论

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