c语言程序设计大作业吧 关注:47贴子:147
  • 0回复贴,共1

汉诺塔问题

只看楼主收藏回复

#include <stdio.h>
void move(char from, char to) {
printf("%c to %c\n", from, to);
}
void hanoi(int n, char a, char b, char c) {
if (n == 1)
move(a, c);
else {
hanoi(n - 1, a, c, b);
move(a, c);
hanoi(n - 1, b, a, c);
}
}
void main() {
int n;
scanf("%d", &n);
hanoi(n, 'A', 'B', 'C');
}


IP属地:湖北1楼2020-12-28 20:32回复