zoukankan      html  css  js  c++  java
  • 汉诺塔

    package com.charles.algorithm;
    
    public class Hanoi {
    
        private String loc;
        private int num;
    
        public Hanoi() {
        }
        
        public Hanoi(Hanoi h) {
            this.num = h.num;
            this.loc = h.loc;
        }
    
        public Hanoi(String loc, int num) {
            this.loc = loc;
            this.num = num;
        }
    
        public static void main(String[] args) {
            
            final int INT_NUM = 3;
            Hanoi A = new Hanoi("A", INT_NUM), B = new Hanoi("B", 0), C = new Hanoi("C", 0);
            int sum = A.num;
            new Hanoi().hanoi(sum, A, B, C);
            System.out.println(A);
            System.out.println(B);
            System.out.println(C);
        }
    
        public void hanoi(int sum, Hanoi A, Hanoi B, Hanoi C) {
    
            if (1 == sum) {
                System.out.println(A.loc + " ------->  " + C.loc);
                A.num--;
                C.num++;
            } else {
                hanoi(sum-1, A, C, B);
                hanoi(1, A, B, C);
                hanoi(sum-1, B, A, C);
            }
        }
    
        public void hanoi(int num, String A, String B, String C) {
    
            if (1 == num) {
                System.out.println(A + " ------->  " + C);
            } else {
                hanoi(num - 1, A, C, B);
                hanoi(1, A, B, C);
                hanoi(num - 1, B, A, C);
            }
        }
    
        public String getLoc() {
            return loc;
        }
    
        public void setLoc(String loc) {
            this.loc = loc;
        }
    
        public int getNum() {
            return num;
        }
    
        public void setNum(int num) {
            this.num = num;
        }
    
        @Override
        public String toString() {
            return "Hanoi [loc=" + loc + ", num=" + num + "]";
        }
    
    }
  • 相关阅读:
    ASP.NET中JSON的序列化和反序列化
    Android:数据存储之SQLite
    转Android:简单联网获取网页代码
    Android:@id和@+id
    linux .run文件安装
    Mysql的Root密码忘记,查看或修改的解决方法(图文介绍)
    网页页面尺寸
    openstack
    br0
    virsh
  • 原文地址:https://www.cnblogs.com/itachy/p/7196691.html
Copyright © 2011-2022 走看看