#include<stdio.h> void Hanoi(int n, char a, char b, char c); void move(char a, int n, char c); int count = 0; int main(void){ int n; char a = 'A', b = 'B', c = 'C'; scanf("%d", &n); Hanoi(n, a, b, c); printf("\n"); return 0; } void Hanoi(int n, char a, char b, char c){ if (n == 1) move(a, 1, c);//如果a上只有一个1,那么直接移到c上 else{ Hanoi(n - 1, a, c, b);//先把a上的n-1个移动到c,b做过渡 move(a, n, c);//把a上留的最后一个移动到c上 Hanoi(n - 1, b, a, c);//再把b上的n-1个移动到c上,a过渡 } } void move(char a, int n, char c){ printf("第%d次移动:%d 从%c 移动到 %c\n", ++count, n, a, c); }