您的位置:首页 > 其它

Hanio (for my memory of Grade One)

2011-08-27 09:54 381 查看
突然想起大一学递归的时候,c++那本教材上的汉诺塔例子,当时不是很理解,今天翻出来看,有一番新感觉,有了新的体会,果然人真的是会成长的。当时觉得有点难的东西现在不会那么困惑了,所以说熟能生巧,时间也是能改变许多东西的。
/*
2011-8-27
author:BearFly1990
*/
package temp;

import java.util.Scanner;

public class Hanio {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
while(true){
int m = console.nextInt();
hanio(m,'a','b','c');
}
}

public static void hanio(int m, char one, char two, char three){
if( m == 1){
move(one , three);
}else{
hanio(m-1,one,three,two);
move(one,three);
hanio(m-1,two,one,three);
}
}
public static void move(char x, char y){
System.out.println(x+"--->"+y);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐