Hanio (for my memory of Grade One)

来源:互联网 发布:discuz 标签 seo 编辑:程序博客网 时间:2024/06/10 21:54
突然想起大一学递归的时候,c++那本教材上的汉诺塔例子,当时不是很理解,今天翻出来看,有一番新感觉,有了新的体会,果然人真的是会成长的。当时觉得有点难的东西现在不会那么困惑了,所以说熟能生巧,时间也是能改变许多东西的。
/*2011-8-27author: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);}}