#include<iostream>
using namespace std;
int k=0;
void move(int n,char x,char y,char z)
{
if(n==1)
cout<<x<<"->"<<z<<endl;
else
{
move(n-1,x,z,y); //将x从z移到y;
cout<<x<<"->"<<z<<endl;
k++;
move(n-1,y,x,z);
k++;
}
}
int main()
{
int n;
cout<<"请输入汉诺塔的层数:";
cin>>n;
cout<<"汉诺塔移动步骤如下:"<<endl;
move(n,'x','y','z');
cout<<"汉诺塔移动的次数:";
cout<<k+1<<endl;
return 0;
}
汉诺塔问题
最新推荐文章于 2024-08-03 21:34:28 发布