emu in blogjava

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            171 隨筆 :: 103 文章 :: 1052 評論 :: 2 Trackbacks

          考試剛剛結束,題目帖出來交流一下。
          Problem Statement
          ????
          You have several identical balls that you wish to place in several baskets. Each basket has the same maximum capacity. You are given an int baskets, the number of baskets you have. You are given an int capacity, the maximum capacity of each basket. Finally you are given an int balls, the number of balls to sort into baskets. Return the number of ways you can divide the balls into baskets. If this cannot be done without exceeding the capacity of the baskets, return 0.
          Each basket is distinct, but all balls are identical. Thus, if you have two balls to place into two baskets, you could have (0, 2), (1, 1), or (2, 0), so there would be three ways to do this.
          Definition
          ????
          Class:
          FillBaskets
          Method:
          countWays
          Parameters:
          int, int, int
          Returns:
          int
          Method signature:
          int countWays(int baskets, int capacity, int balls)
          (be sure your method is public)
          ????

          Constraints
          -
          baskets will be between 1 and 5, inclusive.
          -
          capacity will be between 1 and 20, inclusive.
          -
          balls will be between 1 and 100, inclusive.
          Examples
          0)

          ????
          2
          20
          2
          Returns: 3
          The example from the problem statement.
          1)

          ????
          3
          20
          1
          Returns: 3
          We have only 1 ball, so we must choose which of the three baskets to place it in.
          2)

          ????
          3
          20
          2
          Returns: 6
          We can place both balls in the same basket (3 ways to do this), or one ball in each of two baskets (3 ways to do this).
          3)

          ????
          1
          5
          10
          Returns: 0
          We have more balls than our basket can hold.
          4)

          ????
          4
          5
          10
          Returns: 146

          This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

          posted on 2005-12-13 12:00 emu 閱讀(1117) 評論(1)  編輯  收藏 所屬分類: google編程大賽模擬題及入圍賽真題

          評論

          # emu 解 FillBaskets 2005-12-13 14:20 emu
          public class FillBaskets {
          public int countWays(int baskets, int capacity, int balls){
          if(balls==0) return 1;
          if(baskets == 1)
          if (balls<=capacity) return 1; else return 0;
          int result = 0;
          for(int i=0;i<=capacity&&i<=balls;i++){
          result += countWays(baskets-1,capacity,balls-i);
          }
          return result;
          }
          }
            回復  更多評論
            

          主站蜘蛛池模板: 仁寿县| 旌德县| 突泉县| 临西县| 阿巴嘎旗| 金溪县| 温泉县| 宝兴县| 宝应县| 苍梧县| 甘洛县| 定州市| 永和县| 建湖县| 文安县| 龙南县| 九江县| 汝南县| 台东市| 博乐市| 江门市| 大足县| 淮滨县| 泉州市| 长宁区| 北京市| 卫辉市| 泽州县| 略阳县| 县级市| 永定县| 台东县| 如东县| 乐至县| 镇沅| 藁城市| 奉贤区| 电白县| 大关县| 宁南县| 呈贡县|