递归_汉诺塔问题

public class Tower {
    static int nDisks=3;//盘子的个数
    public static void main(String[] agrs) {
        doTwoers(nDisks, 'A', 'B', 'C');
    }
    //topN表示从上往下数,第topN个盘子,需要从form移动到to上面,innner代表盘子上面的一组盘子需要缓和的地方
    public static void doTwoers(int topN,char from,char inter,char to) {
        if(topN==1)
            System.out.println("Disk 1 from "+ from +" to "+to);
        else {
            doTwoers(topN-1, from, to, inter);
            System.out.println("Disk "+topN+" from "+from+" to "+to);
            doTwoers(topN-1, inter, from, to);
            
        }
    }
    

}

 

posted @ 2017-12-24 15:18  S-Mustard  阅读(200)  评论(0编辑  收藏  举报