#include <stdio.h>
void hanoi(int a, int b, int c, int d){
if (a == 1){
printf("move %d b %d d %d\n", a, b, d );
return ;
}
hanoi(a - 1, b, d, c);
printf("move %d b %d c %d\n", a, b, c);
hanoi(a - 1, d, b, c);
}
int main(){
hanoi(3, 1, 2, 3);
return 0;
}
以上为学习过程
以下为老师教
#include <stdio.h>
void hanoi(int n, int from, int mid, int to) {
if (n == 1) {
printf("Move %d from %d to %d\n", n, from, to);
return;
}
hanoi(n - 1, from, to, mid);
printf("Move %d from %d to %d\n", n, from, to);
hanoi(n - 1, mid, from, to);
}
int main() {
hanoi(3, 1, 2, 3);
return 0;
}