cdr已有序列号代码_已有c语言和代码,求算法和使用说明
#include<stdio.h>
int main()
{
void hanio(int n,char one ,char two,char three);
int m;
printf("输入盘子的数量");
scanf("%d",&m);
printf("移动个%d盘子步骤",m);
hanio(m,'A','B','C');
}
void hanio(int n,char one,char two,char three)
{
void move(char x,char y);
if(1==n) move(one,three);
else
{
hanio(n-1,one ,three,two);
move(one,three);
hanio(n-1,two,one,three);
}
}
void move(char x,char y)
{
printf("%c--->%c\n",x,y);
}
最佳答案
这个就是个简单的汉诺塔的程序,且是用的递归算法,大多C教材上有详细介绍
使用的话也简单,只要输入盘子数量就可以输出移动的过程
当然,程序有一个错及一个小缺陷,
printf("移动个%d盘子步骤",m);最后的括号是中文的,要改为英文的,而最好加个\n
改为
printf("移动个%d盘子步骤\n",m);
程序运行结果
例样输入
输入盘子的数量3
例样输出
移动个3盘子步骤
A--->C
A--->B
C--->B
A--->C
B--->A
B--->C
A--->C
其他回答
暂无其它回答!