学习型 ASP/PHP/ASP.NET 主机 30元/年全能 ASP/PHP/ASP.NET 主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付
发新话题
打印

汉诺塔小程序

汉诺塔小程序

#include"string.h"
#include"stdio.h"

char move(char getone,char putone)
{
   printf("%c-->%c\n",getone,putone);

}
char hanoi(int n,char one,char two,char three)
{
if(n==1)
move(one,three);
else
{
hanoi(n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);
}

}

main()
{

char haoni(int,char,char,char);
int m=3;
char one,two,three;
one=getchar();two=getchar();three=getchar();
hanoi(m,one,two,three);


}



#include"string.h"
#include"stdio.h"

char move(char getone,char putone)
{
   printf("%c-->%c\n",getone,putone);

}
char hanoi(int n,char one,char two,char three)
{
if(n==1)
move(one,three);
else
{
hanoi(n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);
}

}

main()
{

char haoni(int,char,char,char);
int m=3;
char one,two,three;
one=getchar();two=getchar();three=getchar();
hanoi(m,one,two,three);


}









#include"string.h"
#include"stdio.h"

char move(char getone,char putone)
{
   printf("%c-->%c\n",getone,putone);

}
char hanoi(int n,char one,char two,char three)
{
if(n==1)
move(one,three);
else
{
hanoi(n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);
}

}

main()
{

char haoni(int,char,char,char);
int m=3;
char one,two,three;
one=getchar();two=getchar();three=getchar();
hanoi(m,one,two,three);


}

TOP

发了三个一样的
—>〉Sun〈<—

TOP

分治法的应用....不过要想弄的很明白转换过程不是很容易
学习需要安静。。海盗要重新来过。。

TOP

借LZ  的代码
问题::
我有点 不明白的是,这个递归是怎么实现的。
就是它入栈的顺序是怎样
如果我想让每一次的移动都有个编号并显示,该怎么改,
蝴 蝶 颤 动 了 翅 膀 !!!

TOP

void move(int n, char a, char b)
{
    printf("%d: %c -> %c\n", n, a, b);
}

//-----------------------------------------


[ 本帖最后由 cosdos 于 2008-5-3 19:13 编辑 ]
—>〉Sun〈<—

TOP

ls  可没那么简单啊,我弄了半天也弄不出来,
蝴 蝶 颤 动 了 翅 膀 !!!

TOP

这是谭浩强书上的例子原形。

TOP

发新话题