<P>这是个汉诺塔程序,在调试的时候,输入的数字最好不要大于15,因为每大一个数<BR>所得的结果的步骤都会多一倍。如果你有耐心等待结果的话除外。汉诺塔是在欧洲<BR>流行的一种游戏,有a,b,c三个竿。a竿上有若干个由大到小的圆盘,大的在下面,<BR>小的在上面,b,c都是空杆,请你把a杆上的圆盘都倒到别的杆上,或b或c,在倒盘<BR>的过程中不可以大的压小的,实例程序如下:</P>
<P>#include <stdio.h><BR>int i=0;<BR>main()<BR>{<BR> unsigned n;<BR> printf("Please enter the number of discs: ");<BR> scanf("%d",&n);<BR> printf("\tneedle:\ta\t b\t c\n");<BR> movedisc(n,'a','c','b');<BR> printf("\t Total: %d\n",i);<BR> getch();<BR>}<BR>movedisc(n,fromneedle,toneedle,usingneedle)<BR>unsigned n;<BR>char fromneedle,toneedle,usingneedle;<BR>{<BR> if(n>0)<BR> {<BR> movedisc(n-1,fromneedle,usingneedle,toneedle);<BR> i++;<BR> switch(fromneedle)<BR> {<BR> case 'a':switch(toneedle)<BR> {<BR> case 'b':printf("\t[%d]:\t%2d------>%2d\n",i,n,n);<BR> break;<BR> case 'c':printf("\t[%d]:\t%2d------------->%2d\n",i,n,n);<BR> break;<BR> }<BR> break;<BR> case 'b':switch(toneedle)<BR> {<BR> case 'a':printf("\t[%d]:\t%2d<----------%2d\n",i,n,n);<BR> break;<BR> case 'c':printf("\t[%d]:\t\t%2d------>%2d\n",i,n,n);<BR> break;<BR> }<BR> break;<BR> case 'c':switch(toneedle)<BR> {<BR> case 'a':printf("\t[%d]:\t%2d<--------------%2d\n",i,n,n);<BR> break;<BR> case 'b':printf("\t[%d]:\t\t%2d<--------%2d\n",i,n,n);<BR> break;<BR> }<BR> break;<BR> }<BR> movedisc(n-1,usingneedle,toneedle,fromneedle);<BR> }<BR>}</P> |