汉诺塔--java实现

package test;
import java.util.*;
class HanoiTower{
     static void move(int level,String frome,String to)//注意静态方法只能调用静态方法
    {
        System.out.println("frome "+frome+" move "+level+" to "+to);
    }
     //初始 借用 目的
    public static void moveDish(int level,String frome,String inter,String to){  
    //write code here
        if(level==1)
            move(1,frome,to);
        else
        {
            moveDish(level-1,frome,to,inter);     //将前n-1个经过目的  从初始到借用
            move(level,frome,to);                   //将最后一个从初始到目的
            moveDish(level-1,inter,frome,to);         //将前n-1个经过初始 从借用到目的
        }

    }
}
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //write code here
        int n=scan.nextInt();
        HanoiTower.moveDish(n, "A", "B", "C");
    }
}

你可能感兴趣的:(java)