1 #include<stdio.h> 2 3 void Hanoi(int n,char A,char B,char C) 4 { 5 if(n<=0) 6 return; 7 if(n==1) 8 printf("Move %d From %c To %c\n",n,A,C); //只有一个盘子时将其从A移到C 9 else 10 { 11 Hanoi(n-1,A,C,B); //将上面n-1个盘子从A移到B 12 printf("Move %d From %c To %c\n",n,A,C); //将n号盘子从A移到C 13 Hanoi(n-1,B,A,C); //将n-1个盘子从B移到C 14 } 15 } 16 17 int main() 18 { 19 printf("Input the number of dishes:"); 20 int num=0; 21 scanf("%d",&num); 22 Hanoi(num,'A','B','C'); 23 return 0; 24 }