有经典的 汉诺塔问题 算法,贴出来看看。
  我最近,查了好多资料,都不怎么明白的。

解决方案 »

  1.   

    public class Hanoi {    static void hanoi(int n, String startpeg, String middlepeg, String endpeg) {
            if (n == 1)
                System.out.println("move " + startpeg + " to " + endpeg);
            else {
                hanoi(n - 1, startpeg, endpeg, middlepeg);
                System.out.println("move " + startpeg + " to " + endpeg);
                hanoi(n - 1, middlepeg, startpeg, endpeg);
            }
        }    public static void main(String[] args) {
            int n = 3;
            String startpeg = "start";
            String middlepeg = "middle";
            String endpeg = "end";
            System.out.println("The solution for n = " + n);        hanoi(n, startpeg, middlepeg, endpeg);
        }
    }找的
      

  2.   

    作为参数传递给函数的栓名为“start”,“middle”,“end”。一开始提示用户并输入盘子数n然后调用递归函数hanoi一求出将n个盘子以 “start”栓移到“end”的移动步骤。整个算法学要2*2*2….2*2(n个)—1次移动。对于10个盘子的情况,次游戏需1023次移动。在测试的例子中,n=3,移动步骤是2*2*2-1=7. 
      

  3.   

    import javax.swing.JOptionPane;
    public class hanoitower { /**
     * @param args
     */
    public static void main(String[] args) {
    //read number of disks,n
    String intString=JOptionPane.showInputDialog("Enter number of disks:");
    int n=Integer.parseInt(intString);
    moveDisks(n,'A','B','C');
    }
    /**The method for finding the solution to move n disks 
     * form fromTower to toTower with auxTower
     */
    public static void moveDisks(int n,char fromTower,char toTower,char auxTower){
    if(n==1)//stopping condition
    System.out.println("move Disks"+" "+n+" form"+" "+fromTower+" to"+" "+toTower);
    else{
    moveDisks(n-1,fromTower,auxTower,toTower);
    System.out.println("move Disks "+n+" form "+fromTower+" to "+toTower);
    moveDisks(n-1,auxTower,toTower,fromTower);

    }
    }}