7-21打卡

public class HanoiTower {
    public static void main(String[] args) {
        int n = 3; // 圆盘的数量
        char from = 'A'; // 起始柱子
        char aux = 'B'; // 辅助柱子
        char to = 'C'; // 目标柱子

        solveHanoi(n, from, aux, to);
    }

    public static void solveHanoi(int n, char from, char aux, char to) {
        if (n == 1) {
            System.out.println("Move disk 1 from " + from + " to " + to);
            return;
        }

        solveHanoi(n - 1, from, to, aux);
        System.out.println("Move disk " + n + " from " + from + " to " + to);
        solveHanoi(n - 1, aux, from, to);
    }
}

posted @ 2023-07-21 21:50  aallofitisst  阅读(12)  评论(0)    收藏  举报