汉诺塔
# include <stdio.h>
//移动步骤
void move(char A,char B)
{
printf("%c-->%c\n",A,B);
}
//hanno(n,a,b,c)的意思是将n个盘子从a移动到c的过程(借助b座)
void hanno(int n,char A,char B,char C)
{
if(n==1)
move(A,C);
else
{
hanno(n-1,A,C,B);
move(A,C);
hanno(n-1,B,A,C);
}
}
void main()
{
int num;
printf("输入盘子的数量:");
scanf("%d",&num);
printf("移动%d个盘子的步骤是:\n",num);
hanno(num,'A','B','C');
}
//移动步骤
void move(char A,char B)
{
printf("%c-->%c\n",A,B);
}
//hanno(n,a,b,c)的意思是将n个盘子从a移动到c的过程(借助b座)
void hanno(int n,char A,char B,char C)
{
if(n==1)
move(A,C);
else
{
hanno(n-1,A,C,B);
move(A,C);
hanno(n-1,B,A,C);
}
}
void main()
{
int num;
printf("输入盘子的数量:");
scanf("%d",&num);
printf("移动%d个盘子的步骤是:\n",num);
hanno(num,'A','B','C');
}