← 都来码 / Dolaima← 例程索引javascript/school/hanoi.js 原文

javascript/school/hanoi.js · 631 字节 · 下载原文

// 汉诺塔 —— 经典递归练习(打印移动步骤)
// Towers of Hanoi -- the classic recursion exercise (prints the moves)
//
// 来源 / Source: Rosetta Code「Towers of Hanoi」JavaScript 条目(GFDL 1.2 / CC-BY-SA 3.0)
//   https://rosettacode.org/wiki/Towers_of_Hanoi
// 改编 / Adapted: 输出由 console.log 改为本平台的 print_str。
//   Output changed from console.log to this platform's print_str.

function move(n, a, b, c) {
    if (n > 0) {
        move(n - 1, a, c, b);
        print_str("Move disk from " + a + " to " + c + "\n");
        move(n - 1, b, a, c);
    }
}

move(4, "A", "B", "C");