#include<iostream>
using namespace std;
void move(char getone,char putone)//从哪里到哪里
{
cout<<getone<<"-->"<<putone<<endl;
}
void hanoi(int n,char one,char two,char three)//递归函数
{
if(n == 1)
move(one,three);
else
{
hanoi(n-1,one,three,two);//将n-1个盘从one通过three移动到two
move(one,three);//将第n个盘从one移动到three
hanoi(n-1,two,one,three);//将剩下的n-1个盘从two通过one移动到three
}
}
int main()
{
int m;//总数
cout<<"input the number:"<<endl;
cin>>m;
cout<<"the step to moving "<<m<<" diskes"<<endl;
hanoi(m,'A','B','C');
return 0;
}
用递归方法求解Hanoi问题
最新推荐文章于 2024-02-15 18:52:20 发布