一切皆可抽象

          大而無形 庖丁解牛 厚積薄發 滌慮玄覽
             ::  ::  ::  ::  :: 管理

          【原創】蘋果裝箱的算法

          Posted on 2005-10-12 13:52 鋒出磨礪 閱讀(2120) 評論(8)  編輯  收藏 所屬分類: java算法雜談

          如果你有一千個蘋果,有十個箱子,那么現在我要把一千個蘋果放進十個箱子里面,
          放了完之后,不管誰跟我要多少蘋果(包括1000以內的喲),我都可以整箱整箱給
          他,這個問題有解嗎?

          我的思路。
          我從1開始推理的。如果你需要1個,必然有1個箱子裝一個。
          需要兩個,要么給一個箱子裝2個,要么再裝一個的箱子。感覺
          還是裝2個為一箱比較好。那么需要3個時候,就1+2了,那么你需要4個怎么辦。
          是裝一個4個呢,還是裝一個或者2個呢。為了省箱子,我裝4個。
          此時,我發現了規律,1,2,4。那么下一個是8,再下去就是16了,接著32。
          。。。。。。。
          如果有100個,到32的時候,下一個就是64了,總數超過100了。思路改為
          100-(1+2+4+8+16+32)=37。最后一個箱子裝37個。
          當要求的蘋果數在1+2+4+8+16+32=63 和100之間的時候,將算法推諉
          給前面即可。就是先拿37個,剩下的從以前的箱子拼湊。
          至此,推理完畢。

          java實現代碼


          /**
           * <p>Title: </p>
           * <p>Description: </p>
           * <p>Copyright: Copyright (c) 2005</p>
           * <p>Company: </p>
           * @author not attributable
           * @version 1.0
           */

          public class Box {
            private int limit;
            private int code;
            private int applenum;


            public Box(int code,int applenum,int limit) {
               this.limit = limit;
               this.code  = code;
               this.applenum = applenum;

            }
            public int getApplenum() {
              return applenum;
            }
            public int getCode() {
              return code;
            }
            public int getLimit() {
              return limit;
            }
            public void setLimit(int limit) {
              this.limit = limit;
            }
            public void setCode(int code) {
              this.code = code;
            }
            public void setApplenum(int applenum) {
              this.applenum = applenum;
            }

          }


          /**
           * <p>Title: </p>
           * <p>Description: </p>
           * <p>Copyright: Copyright (c) 2005</p>
           * <p>Company: </p>
           * @author not attributable
           * @version 1.0
           */

          public class Apple {
            public Apple() {
            }
            final private int max=100;   // 蘋果總數
            static int   xqs = 99;       // 你需要的蘋果數
            private java.util.Vector Boxvec = new java.util.Vector(); // 箱子和箱子里的蘋果
            private int ai =0;
            private int k = 1;
            public static void main(String args[])
            {
              Apple apple = new Apple();
              apple.InBox(1);
              System.out.println("共需裝"+apple.GetBoxVec().size()+"箱");
              for (int i=0;i<apple.GetBoxVec().size();i++)
              {
                Box box = (Box)apple.GetBoxVec().get(i);
                System.out.println("第 ‘"+box.getCode()+"’ 箱裝'"+box.getApplenum()+"' ");
              }
              apple.GetApple(xqs,apple.GetBoxVec());
            }

            public void GetApple(int x,java.util.Vector vec)
            {
              if (x==1)
              {
               System.out.println("拿第一箱玩去,就1個");
              }
              else
              {
                if (x != 0) {
                  int key = 0;
                  for (int i = 0; i < vec.size() - 1; i++) {
                    Box xbox = (Box) vec.get(i);
                    Box ybox = (Box) vec.get(i + 1);
                    if (x > xbox.getLimit() && x <= ybox.getLimit()) {
                      key = i + 2;
                      System.out.println("拿第" + ybox.getCode() + "箱給他,共" +
                                         ybox.getApplenum() + "個");
                      GetApple(x - ybox.getApplenum(), vec);
                    }
                  }
                }
                else
                  System.out.println("完成");
              }

            }

            public void InBox(int i)
            {
              if ((i+i)>=max)
              {
                Box box = new Box(k,max-ai,max);
                Boxvec.add(box);
              }
              else
              {
                Box box = new Box(k,i,i+ai);
                k++;
                Boxvec.add(box);
                ai = ai + i;
                InBox(i+i);
              }
            }
            public java.util.Vector GetBoxVec()
            {
              return Boxvec;
            }

          }


          評論

          # re: 【原創】蘋果裝箱的算法  回復  更多評論   

          2005-10-14 18:42 by 小虎
          1,1,2,5,10,20,50,100,200,500
          這樣行嗎?

          # re: 【原創】蘋果裝箱的算法  回復  更多評論   

          2005-10-16 11:19 by 鋒出磨礪
          小虎:你的算法,如果我需要40,90,140,190,240,290。。。看來是無法給我了。

          # re: 【原創】蘋果裝箱的算法  回復  更多評論   

          2005-10-16 21:45 by 小虎
          不好意思 沒考慮好^_^!

          # re: 【原創】蘋果裝箱的算法  回復  更多評論   

          2006-08-25 01:13 by LittleBug
          1,2,4,8,16,32,64,128,256,489
          對嗎?

          # re: 【原創】蘋果裝箱的算法  回復  更多評論   

          2006-08-25 01:14 by LittleBug
          但是我不懂其中的規律

          # re: 【原創】蘋果裝箱的算法  回復  更多評論   

          2008-05-20 17:16 by why0603
          9*1 + 9*10 + 9*100 = 999

          哎!其實根據RMB的搭配,可以將
          9*1=(1+2+3+4)*1
          9*10=(1+2+3+4)*10
          ......
          雷同
          這樣的話箱子的數目真是太少了....
          MY GOD,啥思想

          # re: 【原創】蘋果裝箱的算法  回復  更多評論   

          2008-05-20 17:22 by why0603
          再修正一次
          當10*1后,只需要9*10
          so 9*10 = (2+3+4)*10
          so 9*100 = (2+3+4)*100
          so 10*1 = (1+2+3+4)*1

          # re: 【原創】蘋果裝箱的算法  回復  更多評論   

          2009-04-25 09:30 by kitto
          1 1 3 5 10 30 50 100 300 500
          主站蜘蛛池模板: 济宁市| 安阳市| 抚州市| 永泰县| 临泉县| 宁安市| 庆城县| 磐安县| 鱼台县| 渭源县| 杨浦区| 阜阳市| 汝南县| 鲁山县| 临海市| 大田县| 桐庐县| 通化市| 隆尧县| 行唐县| 扶风县| 六盘水市| 平江县| 平阴县| 孟连| 彝良县| 尤溪县| 读书| 东阿县| 循化| 汨罗市| 新余市| 合作市| 大丰市| 宁南县| 都兰县| 阜城县| 建昌县| 句容市| 南平市| 鹤岗市|