分治算法 汉诺塔 java

package graph.fenzhi;

public class Divide {
    public static void main(String[] args) {
        System.out.println("汉诺塔");
        char a,b,c;
        a = 'A';
        b = 'B';
        c = 'C';
        hanuoTower(3,a,b,c);
    }

    public static  void hanuoTower(int num,char a,char b, char c)
    {
        if(num == 1){
            System.out.printf("第%d个盘从%c移动到%c\n",num,a,c);
        } else {
            hanuoTower(num-1,a,c,b);
            System.out.printf("第%d个盘从%c移动到%c\n",num,a,c);
            hanuoTower(num-1,b,a,c);
        }
    }

}


posted @ 2021-08-17 09:27  brady-wang  阅读(33)  评论(0编辑  收藏  举报