递归解决 汉诺塔

 class Han{
int num;
int steps;
Han(int num){
this.num=num;
}
void total()
{
System.out.println("total="+steps);
}
void show(int i, int from, int des, String[] str)
{
int tmp = 3-from-des;//0+1+2=3
if (i==2)
{
System.out.println(1+":"+str[from]+"->"+str[tmp]);
System.out.println(2+":"+str[from]+"->"+str[des]);
System.out.println(1+":"+str[tmp]+"->"+str[des]);
steps+=3;
}else
{
show(i-1, from, tmp, str);
System.out.println(i+":"+str[from]+"->"+str[des]);
show(i-1, tmp, des, str);
steps+=1;
}
}
} public class HelloWorld{
public static void main(String[] args) {
Han h=new Han(5);//set numbers of member
String[] str= {"A","B","C"};//three pillars
h.show(h.num, 0, 2, str);//from pillar 0 to pillar 2
h.total();//2^n-1
}
}

最新文章

  1. 正则匹配中 ^ $ 和 \b 的区别
  2. jquery左右滑动效果的实现
  3. WebForm 回传后如何保持页面的滚动位置
  4. iOS afnetworking最新版报错 没有AFHTTPRequestOperationManager类了
  5. ActionBarActivity: cannot be resolved to a type
  6. Unity获取安卓手机运营商,电量,wifi信号强度,本地Toast,获取已安装apk,调用第三方应用,强制自动重启本应用
  7. mysql登录出现1045错误修改方法
  8. Windows10 环境下安装 ElasticSearch
  9. C# 创建Word项目标号列表、多级编号列表
  10. 两个div不同高度并排显示
  11. JVM远程调试功能
  12. Lab 11-1
  13. Jmeter(三十八)while控制器实现ssh三次重连
  14. JAVA使用Logback发送日志到控制台、文件、ELK的最简单用法
  15. Android:Service
  16. Codeforces 385D - Bear and Floodlight
  17. Data - Hadoop单机配置 - 使用Hadoop2.8.0和Ubuntu16.04
  18. SVN的基本操作
  19. scrapy的简单使用
  20. Vue+webpack报错: listen EADDRINUSE: address already in use :::8080

热门文章

  1. kali Linux 文本图形界面切换遇到的怪问题
  2. 如何使用gcc编译器
  3. uiautomator的坑和AAPT命令方式启动一个应用程序
  4. Google搜索技术
  5. 通过spring来配置某个命令号和执行方法之间的映射
  6. MVC 表单提交提示:已添加了具有相同键的项。
  7. get请求在ie浏览器下有缓存
  8. aspjpeg 半透明描边的实现函数
  9. 客官,您的 Flask 全家桶请收好
  10. java_object的具体使用--上帝