hanoi塔问题(C++语言实现)
代码如下
#include
#include
using namespace std;
void move(string x,string y)
{
cout<<x<<“把盘子交给”<<y<<endl;
}
void hanoi(int n,string one,string two,string three)
{
if(n==1)
move(one,three);
else
{
hanoi(n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);
}
}
int main() {
cout << “请输入需要移动的盘子数:”<<endl;
int plate_number=3;
hanoi(plate_number,“第一个和尚”,“第二个和尚”,“第三个和尚”);
return 0;
}
hanoi塔问题
最新推荐文章于 2024-06-02 19:30:17 发布