汉诺塔

来源:互联网 发布:单片机语音识别 编辑:程序博客网 时间:2024/05/23 22:58

public class hanoi {
public static void main(String[] args) {
hanoi(4);
}
public static void hanoi(int n){
if(n>0){
fun(n,”LEFT”,”MID”,”RIGHT”);
}
}
public static void fun(int n, String from, String mid, String to) {
if(n==1){
System.out.println(“move from ” + from + ” to ” + to);
}else{
fun(n-1, from, to, mid);
fun(1, from, mid, to);
fun(n-1, mid, from, to);
}
}
}

0 0
原创粉丝点击