汉诺塔

public class HanoiTower {

public static void main(String[] args) {

Tower tower = new Tower();
tower.move(64, 'A', 'B', 'C');
}
}

class Tower {
public void move(int num, char a, char b, char c) {

if (num == 1) {
System.out.println(a + "->" + c);
} else {
move(num - 1, a, c, b);
System.out.println(a + "->" + c);
move(num - 1, b, a, c);
}
}
}

posted @ 2021-09-17 14:35  步江伍德  阅读(15)  评论(0)    收藏  举报