Java实现汉诺塔

public class Han {
    private int n = 4;
    private char a = 'a';
    private char b = 'b';
    private char c = 'c';

    public static void main(String[] args) {
        Han towerOfHanoi = new Han();
        System.out.println("开始" + towerOfHanoi.n);
        towerOfHanoi.hanoi(towerOfHanoi.n, towerOfHanoi.a, towerOfHanoi.b,
                towerOfHanoi.c);
        System.out.println("结束" + towerOfHanoi.n);
    }

    private void hanoi(int n, char a, char b, char c) {
        System.out.println("hanoi: " + "n: " + n + " a: " + a + " b: " + b
                + " c: " + c);
        if (n > 0) {
            this.hanoi(n - 1, a, c, b);
            System.out.println("移动第" + n + "个," + a + "->" + c);
            this.hanoi(n - 1, b, a, c);
        }
    }
}
    原文作者: 汉诺塔问题
    原文地址: https://blog.csdn.net/xiaxl/article/details/72568572
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞