您的位置:首页 > 编程语言 > C语言/C++

递归解决汉诺塔问题C++

2018-01-26 15:56 423 查看
#include <iostream>
using namespace std;
void TowersOfHanoi(int n, int x, int y, int z)
{ //把n 个碟子从塔x 移动到塔y,可借助于塔z
if (n > 0) {
TowersOfHanoi(n-1, x,z,y);
cout << "Move top disk from tower " << x <<" to top of tower " << y << endl;
TowersOfHanoi(n-1, z, y, x);}
}
int main()
{
TowersOfHanoi(10,1,2,3);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: