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);
}
}
}
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交