#include <iostream>
using namespace std;
int i = 1;
void move(int n, char from, char to) {
printf("第%d步:\t将编号为第%d的盘子从%c-->%c\n", i++, n, from, to);
}
void hanoi(int n, char from, char depend_on, char to) {
if (n == 1)
move(1, from, to);
else {
hanoi(n - 1, from, to, depend_on);
move(n, from, to);
hanoi(n - 1, depend_on, from, to);
}
}
int main() {
cout << "请输入盘子数" << endl;
int n;
cin >> n;
char x = 'A', y = 'B', z = 'C';
cout << "盘子的移动情况如下" << endl;
hanoi(n, x, y, z);
return 0;
}
汉诺塔问题
最新推荐文章于 2024-07-10 22:49:54 发布