/* Hanoi(汉诺)塔的问题。 函数递归方法的经典运用。 */ #include <stdio.h> void main() { void hanoi(int n,char one,char two ,char three); //对函数hanoi的声明 int m; printf("input the number of diskes:"); scanf("%d",&m); printf("The step to moveing %d diskes:/n",m); hanoi(m,'A','B','C'); } void hanoi(int n,char one,char two,char three) //定义hanoi函数 { void move(char x,char y); //对move函数的声明 if(n==1) move(one,three); else { hanoi(n-1,one,three,two); move(one,three); hanoi(n-1,two,one,three); } } void move(char x,char y) //定义move函数 { printf("%c-->%c/n",x,y);; } /*运行结果如下: --------------------------------------------------------------------------- input the number of diskes:5 The step to moveing 5 diskes: A-->C A-->B C-->B A-->C B-->A B-->C A-->C A-->B C-->B C-->A B-->A C-->B A-->C A-->B C-->B A-->C B-->A B-->C A-->C B-->A C-->B C-->A B-->A B-->C A-->C A-->B C-->B A-->C B-->A B-->C A-->C --------------------------------------------------------------------------- */