汉诺塔问题:打印n层汉诺塔从最左边移动到最右边的全部过程。

    1. public static void hanoi(int n) {
    2. if (n > 0) {
    3. func(n, "left", "right", "mid");
    4. }
    5. }
    6. public static void func(int N, String from, String to, String other) {
    7. if (N == 1) { // base case 最上面的圆盘想怎么动就怎么动
    8. System.out.println("Move 1 from " + from + " to " + to);
    9. } else {
    10. func(N - 1, from, other, to);
    11. System.out.println("Move " + N + " from " + from + " to " + to);
    12. func(N - 1, other, to, from);
    13. }
    14. }