【C语言】汉诺塔 移动盘子
// 汉诺塔 移动盘子
#include <stdio.h>
int step = 0;
int main() {
void hanoi(int n, char one, char two, char three);
int m;
printf("请输入盘子数量:");
scanf("%d", &m);
hanoi(m, 'A', 'B', 'C');
return 0;
}
void move(int n, char x, char y) {
step++;
printf("\nStep %d:Move disk%d from %c to %c", step, n, x, y);
}
void hanoi(int n, char one, char two, char three) {
void move(int n, char x, char y);
if (n==1) {
move(n, one, three);
} else {
hanoi(n-1, one, three, two);
move(n-1, one, three);
hanoi(n-1, two, one, three);
}
}
扫描二维码,在手机上阅读
收藏