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

#include 
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;
}

你可能感兴趣的:(C++)