#include<stdio.h>
int count=0;
void Move(int n, char a, char b)
{
count++;
printf("第%d次移动 Move %d: Move from %c to %c \n",count,n,a,b);
}
void Hanoi(int n, char a, char b, char c)
{
if (n == 1)
{
Move(n, a, c);
}
else
{
Hanoi(n - 1, a, c, b);
Move(n, a, c);
Hanoi(n - 1, b, a, c);
}
}
int main()
{
int n=0;
printf("汉诺塔的层数:\n");
scanf("%d",&n);
Hanoi(n, 'A', 'B', 'C');
return 0;
}
汉诺塔
最新推荐文章于 2023-10-13 12:45:09 发布