Bootstrap

汉诺塔递归函数

#include <stdio.h>
void move(char x,char y)
{
 static i=0;/*定义为static静态变量,使下次执行本语句时,i的值保持前一次运算的结果而不会被初始为0*/
 i++;
 printf ("%c-->%c  ",x,y);
 if(i%10==0) printf("/n");/*控制换行,每输出10个步骤后换一次行*/
}

void hanoi(int n,char one,char two,char three)
{
 if(n==1) move(one,three);
 else
  {
   hanoi(n-1,one,three,two);
   move(one,three);
   hanoi(n-1,two,one,three);
  }
}

void main()
{
 int m;
 printf ("Input the number of diskes:");
 scanf ("%d",&m);/*输入待移动的盘子数*/
 printf ("The step to moving %3d diskes:/n",m);
 hanoi(m,'A','B','C');

;