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???}
?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???}