用递归的方法编写 Hanoi塔问题,观察递归调用过程

来源:百度知道 编辑:UC知道 时间:2024/05/17 07:13:14
请高手帮帮忙!!

#include<iostream.h>

void move(char ch1, char ch2) {
cout<<ch1<<"->"<<ch2<<' ';
}

void hanoi(int n, char a, char b, char c) {
if (n==1)
move (a,c);
else {
hanoi (n-1,a,c,b);
move (a,c);
hanoi (n-1,b,a,c);
}
}

void main() {
int m;
cout<<"Enter the number of disk to move:\n";
cin>>m;
cout<<"The step to moving "<<m<<" disk:\n";
hanoi (m,'A','B','C');
cin>>m;
}

曾经用C语言编写过,现在不记得了``哈哈``