专利代理人考试培训班:怎么用递归的方法做汉诺塔c程序?

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/27 13:48:07

这里有一个源代码,你不妨参考一下:http://www.oldlinux.org/LB5000XP//usr/20/20_8_5.zip

#include <stdio.h>

//递归汉诺塔程序
//参数定义:n为移动的盘子数,from指开始所在盘,tmp为中间盘,to为目标盘
void haoii(int n,int from,int tmp,int to)
{
if (n==0) return;
//将n-1个盘子以目标盘为中介,从开始盘递到中间盘
haoii(n-1,from,to,tmp);
将最后一个盘子移到目标盘
printf("Move %d from %d to %d\n",n,from,to);
//将中间盘上的n-1个盘子移到目标盘
haoii(n-1,tmp,from,to);
}

main()
{
haoii(32,1,2,3);
getchar();
return 0;
}