返回信息流#include <iostream>
using namespace std;
void move (int n,char source, char target)
{
cout<<n<<"\t"<<source<<"---->"<<target<<endl;
}
void hanoi(int n,char A,char B,char C)
{
if (n==1)
move(1,A,C);
else
hanoi(n-1,A,C,B);
move (n,A,C);
hanoi(n-1,B,A,C);
}
void main()
{
int num;
cin>>num;
hanoi(num,'A','B','C');
}
这是一条镜像帖。来源:北邮人论坛 / cpp / #17220同步于 2008/12/8
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
这个汉诺塔哪里错了?
qhbupt
2008/12/8镜像同步4 回复
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
else少了一个{}
else
{
hanoi(n-1,A,C,B);
move (n,A,C);
hanoi(n-1,B,A,C);
}
【 在 qhbupt 的大作中提到: 】
: #include <iostream>
: using namespace std;
: void move (int n,char source, char target)
: ...................
哦,x谢了~要是没有是不是只执行紧跟在else后面的吧
【 在 xieys 的大作中提到: 】
: else少了一个{}
: else
: {
: ...................
如果n=1的话
执行:move(1,A,C);
move (n,A,C);
hanoi(n-1,B,A,C);
否则执行:
hanoi(n-1,A,C,B);
move (n,A,C);
hanoi(n-1,B,A,C);
PS:刚才错误的情况
【 在 qhbupt 的大作中提到: 】
: 哦,x谢了~要是没有是不是只执行紧跟在else后面的吧