首页 > 其他 > 详细

经典递归小程序--汉诺塔

时间:2017-01-19 13:04:34      阅读:166      评论:0      收藏:0      [点我收藏+]

#include <stdio.h>

/*

思路:1、将1到n-1号盘子借助C移到B上

 2、将n号盘子移到C上

 3、将1到n-1号盘子借助A移到C上

  */

//初始化步数

int i = 0;


void move(int,char,char);

void hannuota(int,char,char,char);


void main(void){

int n;

printf("请输入汉诺塔盘子的个数:");

scanf("%d",&n);

hannuota(n,‘A‘,‘C‘,‘B‘);

}


//移动盘子方法

void move(int n,char from,char to){

printf("第%d步,将%d号盘子从%c移到%c\n",++i,n,from,to);

}

//汉诺塔递归算法

void hannuota(int n,char from,char to,char depend_on){

if(n == 1){

move(1,from,to);

}else{

hannuota(n-1,from,depend_on,to);

move(n,from,to);

hannuota(n-1,depend_on,to,from);

}

}


经典递归小程序--汉诺塔

原文:http://zhuangyong.blog.51cto.com/12459964/1893121

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!