hanoirecursive.cpp
来自「这是数据结构、算法与应用-C++语言描述的代码」· C++ 代码 · 共 24 行
CPP
24 行
// Recursive Towers of Hanoi
#include <iostream>
using namespace std;
void towersOfHanoi(int n, int x, int y, int z)
{// Move the top n disks from tower x to tower y.
// Use tower z for intermediate storage.
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);
}
}
void main(void)
{
cout << "Moves for a three disk problem are" << endl;
towersOfHanoi(3,1,2,3);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?