递归实现汉诺塔问题

public class HanoiTower
{ 
    public static void main(String[] args)throws Exception
    {
        hanoiTower(64,"X","Y","Z");
    } 
    
    public static void hanoiTower(int n, String x, String y, String z)
    {
        if(n == 1)
        {
            System.out.println(x+"-->"+z);
            return;
        }
        else
        {
            hanoiTower(n-1,x,z,y);
            System.out.println(x+"-->"+z);
            hanoiTower(n-1,y,x,z);
        }
    }
}

 

posted on 2016-07-24 19:40  kuillldan  阅读(173)  评论(0编辑  收藏  举报