Hanoi tower H(n, from, to. using )并显示n = 3和n = 4的输出。初始值从from= 1,to= 2,using= 3 求个直接能用的代码谢谢大神了
1 回答
白猪掌柜的
TA贡献1893条经验 获得超10个赞
public class Main {
public static void hanoi(int n, String from, String to, String using) {
if (n == 1) {
move(from, using);
} else {
hanoi(n - 1, from, using, to);
move(from, using);
hanoi(n - 1, to, from, using);
}
}
private static void move(String from, String target) {
System.out.println("move:" + from + "-->" + target);
}
public static void main(String[] args) {
System.out.println("移动汉诺塔的步骤:");
hanoi(3, "1", "2", "3");
}
}
添加回答
举报
0/150
提交
取消