标题:Hanoi塔问题
只看楼主
dmonkey
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-11-12
得分:0 
递归
2007-11-17 10:13
purson
Rank: 1
来 自:天堂
等 级:新手上路
帖 子:25
专家分:0
注 册:2007-9-12
得分:0 

给你一个源代码,希望对你有所帮助,你可以运行一下.
如下:

#include<stdio.h>
void movedisc(unsigned n,char fromneedle,char toneedle,char usingneedle);
int i=0;
int main()
{
unsigned n;
printf("please enter the number of disc:");
scanf("%d",&n);
printf("\tneedle:\ta\t b\t c\n");
movedisc(n,'a','c','b');
printf("\t Total: %d\n",i);
}
void movedisc(unsigned n,char fromneedle,char toneedle,char usingneedle)
{
if(n>0)
{
movedisc(n-1,fromneedle,usingneedle,toneedle);

++i;
switch(fromneedle)
{
case 'a': switch(toneedle)
{
case 'b': printf("\t[%d]:\t%2d.........>%2d\n",i,n,n);
break;
case 'c': printf("\t[%d]:\t%2d...............>%2d\n",i,n,n);
break;
}
break;
case 'b': switch(toneedle)
{
case 'a': printf("\t[%d]:\t%2d<...............>%2d\n",i,n,n);
break;
case 'c': printf("\t[%d]:\t %2d........>%2d\n",i,n,n);
break;
}
break;
case 'c': switch(toneedle)
{
case 'a': printf("\t[%d]:\t%2d<............%2d\n",i,n,n);
break;
case 'b': printf("\t[%d]:\t%2d<........%2d\n",i,n,n);
break;
}
break;
}
movedisc(n-1,usingneedle,toneedle,fromneedle);

}
}


2007-11-18 22:12
等着绵羊的狼
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2007-11-18
得分:0 
。好!!好程序!!
2007-11-18 23:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-186276-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.046936 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved