A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

本帖最后由 黑马刘涛 于 2012-7-16 18:07 编辑
  1. class HanoiDemo
  2. {

  3.         public static void main(String[] args)
  4.         {
  5.                 Hanoi h = new Hanoi();
  6.                 h.hanoi(5,'A','B','C');
  7.         }
  8. }
  9. class Hanoi
  10. {
  11.         private static int count = 0;//计数器
  12.         //将塔座a上按直径由小到大且自上而下编号为1到n的n个
  13.         //圆盘搬到塔座c上,b可用于辅助塔座
  14.         public void hanoi(int n,char a,char b,char c)
  15.         {
  16.                 if(n == 1)
  17.                         move(a,1,c);//将编号为1的圆盘从塔座a搬到塔座c上
  18.                 else
  19.                 {
  20.                         //将塔座a上编号为1到n-1的圆盘搬到塔座b,c作为辅助塔
  21.                         hanoi(n-1,a,c,b);
  22.                         //将塔座a上编号为n的圆盘搬到塔座c上
  23.                         move(a,n,c);
  24.                         //将塔座b上编号为1到n-1的圆盘搬到塔座c上,a作为辅助塔
  25.                         hanoi(n-1,b,a,c);
  26.                 }
  27.         }
  28.         //搬动操作
  29.         public void move(char a,int n,char c)
  30.         {
  31.                 System.out.println("第"+(count++)+"次搬运:"+"将塔座"+a+"上的圆盘"+n+"搬到塔座"+c);
  32.         }
  33. }
复制代码
数据结构课程中关于栈降到的递归问题

评分

参与人数 1技术分 +1 收起 理由
蒋映辉 + 1

查看全部评分

回复 使用道具 举报
这么多人都写出来了,那我也要试试,下面是我的程序:
class HanoiTower {
         public static void mover(int n,char a,char b,char c)
         {
                 if(n ==1)
           {
                         System.out.println("把"+1 + "号盘子从" + a + "移到" + c);
                 }
         else
        {
                       mover(n-1, a, c, b);
                         System.out.println("把"+n + "号盘子从" + a + "移到" + c);
                       mover(n-1, b, a, c);
                 }
          }
         
      public static void main(String[] args)
      {
            
                int num = 10;               
              mover(num, 'A', 'B', 'C');

         }
}
结果太长,就不在这里显示图片了。

评分

参与人数 1技术分 +1 收起 理由
蒋映辉 + 1

查看全部评分

回复 使用道具 举报
public class Hnt
{
       
        /**
         * @param args
         */
        public static void main(String[] args)
        {
       
                // TODO Auto-generated method stub
                new Hnt().hanoi(4, "a", "b", "c");
               
        }
       
        // num是盘子个数,A是起始柱子,B是目标柱子,C是临时柱子
//用四个盘子进行解释的。
        private void hanoi(int num, String A, String B, String C)
        {
       
                if (num > 0)
                {
                        hanoi(num - 1, A, C, B);//将上面三个放到目标柱子上
                        System.out.println(A + "---->" + B);//将最下面一下移动到目标柱子
                        hanoi(num - 1, C, B, A);//将临时柱子的三个移动的目标柱子上
                }
        }
       
}

评分

参与人数 1技术分 +1 收起 理由
蒋映辉 + 1 好吧 哥们儿,你圆满了....

查看全部评分

回复 使用道具 举报
  1. public class Hanio {
  2.         public static void main(String[] args){
  3.                 int i=3;
  4.                 char a ='A',b='B',c='C';
  5.                 hanio(i,a,b,c);
  6.         }
  7.         public static void hanio(int n,char a,char b,char c){
  8.                 if(n==1)
  9.                         System.out.println("移动"+n+"号盘子从"+a+"到"+c);
  10.                 else{
  11.                         hanio(n-1,a,c,b);//把上面n-1个盘子从a借助b搬到c
  12.                         System.out.println("移动"+n+"号盘子从"+a+"到"+c);//紧接着直接把n搬动c
  13.                         hanio(n-1,b,a,c);//再把b上的n-1个盘子借助a搬到c
  14.                 }
  15.         }
  16. }
复制代码
我也来写一个

评分

参与人数 1技术分 +1 收起 理由
蒋映辉 + 1

查看全部评分

回复 使用道具 举报
import java.io.*;

public class Hanoi {
    public static void main(String args[]) throws IOException {
        int n;
        BufferedReader buf;
        buf = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("请输入盘数:");
        n = Integer.parseInt(buf.readLine());

        Hanoi hanoi = new Hanoi();
        hanoi.move(n, 'A', 'B', 'C');
    }

    public void move(int n, char a, char b, char c) {
        if(n == 1)
            System.out.println("盘 " + n + " 由 " + a + " 移至 " + c);
        else {
            move(n - 1, a, c, b);
            System.out.println("盘 " + n + " 由 " + a + " 移至 " + c);
            move(n - 1, b, a, c);
        }
    }
}


想法不难!
回复 使用道具 举报
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);
        }
}
回复 使用道具 举报






import javax.swing.JOptionPane;

/*
递归原理:调用自身实现递归

移动次数f(n)
f(1)=1;
f(2)=3;
f(3)=7;
==>f(k+1)=2*f(k)+1;
==>f(n) = 2^n-1;
*/
public class Hanoi
{
        private static final String b = "diskB";
        private static final String c = "diskC";
        private static final String a = "diskA";
        static String from=a;
        static String to=c;
        static String mid=b;
        public static void main(String[] args)
        {
                //显示对话框
                String input =
                        JOptionPane.showInputDialog("请输入要移动的盘子个数");

                int num = Integer.parseInt(input);
                move(num,from,mid,to);

        }
        private static void move(int num, String from2, String mid2, String to2)
        {

                if(num==1)
                { //当num是1的时候,跳出递归
                System.out.println("move disk 1 from "+from2+" to "+to2);

                }
                else
                {
                        //调用自身方法
                        move(num-1,from2,to2,mid2);
                        System.out.println("move disk "+num+" from "+from2+" to "+to2);
                        move(num-1,mid2,from2,to2);

                }
        }

}

评分

参与人数 1技术分 +1 收起 理由
蒋映辉 + 1

查看全部评分

回复 使用道具 举报
12
您需要登录后才可以回帖 登录 | 加入黑马