怒了~~~~~~汉诺塔程序怎么写

Lokioo 2003-04-07 06:13:13
汉诺塔程序怎么写~~~~
我怎么递过不出正确结果
...全文
28 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lbaby 2003-04-07
  • 打赏
  • 举报
回复
呵呵,参看:
http://expert.csdn.net/Expert/topic/1556/1556827.xml?temp=.6454126

用tc2.0或gcc编译
Lokioo 2003-04-07
  • 打赏
  • 举报
回复
void hanno( int num,char& a,char& b,char& c)
{
if(num!=1){

hanno(num-1,a,b,c);//num-1 个从a--b
hanno(1,a,c,b);
hanno(num-1,b,c,a);//num-1 个从b--c
}
else{

cout<<a<<"--->"<<b<<endl;

}
}

//错在那里!~~~~~~~~
wxy001 2003-04-07
  • 打赏
  • 举报
回复
#include <iostream.h>
#include <time.h>
#include <stdio.h>
void move_tower(int d,char f,char t,char a)
{
if(d==1){

cout<<"Move d1 from "<<f<<" to "<<t<<endl;
}else{
move_tower(d-1,f,a,t);
cout<<"move d"<<d<<" from "<<f<<" to "<<t<<endl;
move_tower(d-1,a,t,f);
}
return;
}
int main(void)
{
clock_t start, end;
start = clock();

move_tower(14,'A','C','B');

end = clock();

cout<<"The time is"<<((end-start)/ CLK_TCK)<<endl;
printf("The time was: %f\n", (end - start) / CLK_TCK);


return 0;
getchar();
}
我好久没用c++了,你看看,这是我很早以前写的 大概在4年前
kbsoft 2003-04-07
  • 打赏
  • 举报
回复
ft!我还以为非递归呢
#include <iostream>
using namespace std;

void Hanoi(int n, int A, int B, int C)
{
if (n == 0) return;
Hanoi(n-1, A, C, B);
cout << A << "-->" << C << endl;
Hanoi(n-1, B, A, C);
}

void main()
{
int n;
cin >> n;
Hanoi(n, 1, 2, 3);
}

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧