#include <stdio.h>
int step=0;
int main()
{
void hano(int n,char one,char two,char three);
int n;
printf("Input the number of dish :");
scanf("%d",&n); //输入盘子的总个数
printf("The step of move %d dish is :\n",n);
hano(n,'A','B','C'); //开始移动汉诺塔
printf("Totol step is %d\n",step); //打印移动的次数
return 0;
}
void hano(int n,char one,char two,char three)
{
void move(char one ,char two);
if(n==1)
move(one,three);
else
{
hano(n-1,one,three,two);
move(one,three);
hano(n-1,two,one,three);
}
}
void move(char one,char two)
{
printf("%c-->%c\n",one,two);
step++;
}
汉诺塔
最新推荐文章于 2018-11-04 20:19:30 发布