/*
汉诺塔问题:有三个座,A座上有若干个盘子,盘子大小不等,大的在下,小的在下,问如何移动,将A座的盘子移动到C座上,移动过程中始终保持大盘子在下,小盘子在上。
*/
#include<stdio.h>
int main()
{
void hanoi(int n,char one,char two,char three);
int n;
printf(“The number of disks is:”);
scanf("%d",&n);
hanoi(n,‘A’,‘B’,‘C’);
return 0;
}
void hanoi(int n,char one,char two,char three)
{
void move(char a,char b);
if(n==1)
move(one,three);
else
{
hanoi(n-1,one,three,two);
move(one,three);
hanoi(n-1,two,one,three);
}
}
void move(char a,char b)
{
printf("%c–>%c\n",a,b);
}
汉诺塔问题
最新推荐文章于 2024-03-06 23:24:36 发布