汉诺塔问题
思路:从A移动到B,从B移动到C(总体思路)
#include <stdio.h> #include <stdlib.h> void Move(int n,char x,char y) { printf("move %d from %c to %c\n",n,x,y); } void Hannoi(int n,char a,char b,char c) { if(n==1) { Move(1,a,c); } else { Hannoi(n-1,a,c,b); Move(n,a,c); Hannoi(n-1,b,a,c); } } int main() { int n; while(scanf("%d",&n)!=EOF) { Hannoi(n,'a','b','c'); } return 0; }