//汉诺塔问题是一个典型的使用递归算法解决的问题
#include <stdio.h>
int main()
{
void hanoi(int n,char one,char two,char three);
int m;
printf("the number of disks:");
scanf("%d",&m);
printf("move %d diskes:\n",m);
hanoi(m,'A','B','C');
}
void hanoi(int n,char one,char two,char three)
{
void move (char x,char y);
if(n==1)//临界条件
move(one,three);
else//递归公式
{
hanoi(n-1,one,three,two);//如果想把最大的一个圆盘由A——>C,首先要把它上面的n-1个圆盘由A->B
move(one,three);
hanoi(n-1,two,one,three);//最后再把n-1个圆盘由B->C
}
}
void move(char x,char y)
{
printf("%c-->%c\n",x,y);
}c语言 递归算法解决汉诺塔问题
最新推荐文章于 2025-09-24 21:44:51 发布
本文介绍了一个经典的递归算法案例——汉诺塔问题,并通过C语言实现展示了其解决方案。递归算法通过将复杂问题分解为更小规模的相同问题来求解,汉诺塔问题就是这一思想的典型应用。
1343

被折叠的 条评论
为什么被折叠?



