#include<stdio.h>
#include<windows.h>
void Hanoi(int n, char a, char b, char c);
void Move(int n, char a, char b);
int main()
{
int n = 3;
Hanoi(n, 'A' ,'B' ,'C');
system("pause");
return 0;
}
void Hanoi(int n, char a, char b, char c)
{
if(n==1)
{
Move(n,a,b);
}
else
{
Hanoi(n-1,a,c,b);
Move(n,a,b);
Hanoi(n-1,c,b,a);
}
}
void Move(int n, char a, char b)
{
printf("Move %d: from %c to %c\n", n, a, b);
}
【C语言】Hanoi塔练习
最新推荐文章于 2024-08-30 15:57:36 发布