汉诺塔
来源:互联网 发布:我的世界手机版机械js 编辑:程序博客网 时间:2024/05/31 19:34
package offer;
/**
* 需求: 汉若塔问题,就是把A柱子上面从大到小一次叠放的盘子借助B柱移到C柱上去,规则是一次只能移动一个盘子,大盘子不能放到小盘子之上
* 思路:递归
* 1.先将A上面的n-1个盘子,移到B柱子上
* 2.然后把A上最大的一个盘子放到C上
* 3、然后把B上面的n-1个盘子移到A上
* @author lpc
*/
public class Hannou {
public static void moveDish(int level, char from, char inter, char to){
if(level == 1){
System.out.println("从" + from + "移动盘" + level +"到" + to);
}else{
moveDish(level - 1, from, to, inter);
System.out.println("从" + from + "移动盘" + level +"到" + to);
moveDish(level -1, inter, from, to);
}
}
/**
*
* @param args
*/
public static void main(String[] args) {
int nDisk = 5;
moveDish(nDisk, 'A', 'B', 'C');
}
}
/**
* 需求: 汉若塔问题,就是把A柱子上面从大到小一次叠放的盘子借助B柱移到C柱上去,规则是一次只能移动一个盘子,大盘子不能放到小盘子之上
* 思路:递归
* 1.先将A上面的n-1个盘子,移到B柱子上
* 2.然后把A上最大的一个盘子放到C上
* 3、然后把B上面的n-1个盘子移到A上
* @author lpc
*/
public class Hannou {
public static void moveDish(int level, char from, char inter, char to){
if(level == 1){
System.out.println("从" + from + "移动盘" + level +"到" + to);
}else{
moveDish(level - 1, from, to, inter);
System.out.println("从" + from + "移动盘" + level +"到" + to);
moveDish(level -1, inter, from, to);
}
}
/**
*
* @param args
*/
public static void main(String[] args) {
int nDisk = 5;
moveDish(nDisk, 'A', 'B', 'C');
}
}
0 0
- 汉诺塔
- 汉诺塔
- 汉诺塔
- 汉诺塔
- 汉诺塔
- 汉诺塔
- 汉诺塔
- 汉诺塔
- 汉诺塔
- 汉诺塔
- 汉诺塔
- 汉诺塔
- 汉诺塔
- 汉诺塔
- 汉诺塔
- 汉诺塔
- 汉诺塔
- 汉诺塔
- Inflater的作用
- 产品开发摘要
- Tools介绍:Dependency Walker 介绍
- 代理IP 有效性检测
- 学习springmvc的基础搭建
- 汉诺塔
- 有符号二进制数的乘法
- Unity3D “xx AnimationEvent has no function name specified!”解决方案
- 409. Longest Palindrome
- HttpServlet需要导入的Jar包
- java获取北京时间不可靠
- Map集合的四种遍历方式
- java基础之面向对象(三)
- hadoop 的调优