柱。
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 }