Wednesday, March 31, 2004

 

Tower of Hanoi

Tower of Hanoi 的程式碼
代 swanky po~
上課專心啦~

public class Hanoi {
    public static void hanoi(int number) {
        hanoi(number, "a", "b", "c");
    }

    public static void hanoi(int number, String a, String b, String c) {
        if (number == 0) {
            return;
        }

        hanoi(number - 1, a, c, b);
        System.out.println(a + "->" + c);
        hanoi(number - 1, b, a, c);
    }

    public static void main(String[] args) {
        hanoi(3);
    }
}

執行結果:
a->c
a->b
c->b
a->c
b->a
b->c
a->c
由 shumi 發表於 March 31, 2004 11:34 PM

Comments: Post a Comment



<< Home