解题思路:
注意事项:
参考代码:
#include <iostream> using namespace std; void move(int &n, char &x, char &y) { //执行移动盘子的操作 printf("Move %d from %c to %c\n", n, x, y); } void hanoi(int n,char one, char two, char three) { //将n个盘从one座借助two座,移到three座 if(n==1) {//只有 1 个盘直接移,不需要借助 move(n, one, three); } else { hanoi(n-1, one, three, two); move(n, one, three); hanoi(n-1, two, one, three); } } int main() { int n; scanf("%d",&n); hanoi(n, '1', '2', '3'); return 0; }
0.0分
5 人评分