现在的位置: 首页 > 综合 > 正文

关于汉诺塔有感 经典递归解决汉诺塔!

2018年03月19日 ⁄ 综合 ⁄ 共 461字 ⁄ 字号 评论关闭

汉诺塔的递归性

public class Hanoi {

    
    public void hanoi(int n,char origin, char assist,char destination){
        
    if(n==1){
    move(origin,destination);
    }else{
    hanoi(n-1,origin,destination,assist);
    move(origin,destination);
    hanoi(n-1,assist,origin,destination);
    }
    }
    
    //Print the route of the movement
    private void move(char origin,char destination){
    System.out.println("Direction:"+origin+"--->"+destination);
    }
    
    public static void main(String[]args)
    {
        Hanoi hanoi=new Hanoi();
        hanoi.hanoi(64,'A','B','C');
    }
    
}

怎么想到的呢?

经典递归解决汉诺塔!

【上篇】
【下篇】

抱歉!评论已关闭.