Hanoi tower H(n, from, to. using )并顯示n = 3和n = 4的輸出。初始值從from= 1,to= 2,using= 3 求個直接能用的代碼謝謝大神了
1 回答

白豬掌柜的
TA貢獻(xiàn)1893條經(jīng)驗 獲得超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
提交
取消