`
dugu108
  • 浏览: 23238 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

递归汉诺塔

阅读更多

递归汉诺塔

 

 

public class HanoiQuestion {
    private static void move(char from, char to) {
        System.out.println("move the top plate from " + from + " to " + to);
    }

    public static void hanoi(char from, char to, char mid, int index) {
        if (index == 1)
            move(from, to);
        else {
            hanoi(from, mid, to, index - 1);
            move(from, to);
            hanoi(mid, to, from, index - 1);
        }
    }

    public static void main(String[] args) {
        HanoiQuestion.hanoi('A', 'B', 'C', 3);
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics