递归之经典:汉诺塔

来源:互联网 发布:如何成为英雄知乎 编辑:程序博客网 时间:2024/05/16 04:55


递归之经典:汉诺塔

public class hanoitower {


public static void main(String[] args) {
dototower(5, 'A', 'B', 'C');
}

public static void dototower(int N,char from, char inner, char to){
if(N == 1){
System.out.println("盘子      "+N+" 从    "+from+" 到    "+to);
}else {
dototower(N-1, from, to, inner);
System.out.println("盘子      "+N+" 从    "+from+" 到    "+to);
dototower(N-1, inner, from, to);
}
}


}

0 0