#include <stdio.h> #include <stdlib.h> void hanoi(int n, char a, char b, char c); void hanoi(int n, char a, char b, char c) { if(n == 1) { printf("%d:%c to %c, ", n, a, c); } else{ hanoi(n-1, a, c, b); //把A上n-1个盘子通过借助辅助塔(C塔)移到了B上 printf("%d:%c to %c, ", n, a, c); hanoi(n-1, b, a, c); //把B上n-1个盘子通过借助辅助塔(A塔)移到了C上 } } int main(void) { hanoi(3, 'A', 'B', 'C'); return EXIT_SUCCESS; }
用的是递归方法