在Java中使用递归的河内塔

时间:2019-08-18 15:17:39

标签: java recursion

河内塔中的每个堆栈如何出现问题,如何逐行执行,我不太了解每个堆栈的创建方式?

public class TowerOfHanoi {

    public static void towerOfHanoi(int disks, char source, char auxiliary, char destination) {
        if (disks == 0) {
            return;
        }

        towerOfHanoi(disks - 1, source, destination, auxiliary);
        System.out.println(source + " " + destination);
        towerOfHanoi(disks - 1, auxiliary, source, destination);

    }

    public static void main(String[] args) {
        towerOfHanoi(4, 'a', 'b', 'c');
    }
}

1 个答案:

答案 0 :(得分:0)

这应该工作:

<th>