1.问题描述


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.
你有几个同样的球,你希望把它放到几个篮子里。每个篮子有相同的容量。给出int 型的baskets,代表篮子的数量。给出int型的 capacity,代表每个篮子的最大容量。给出int型的balls,表示归类到篮子里的球的数量。返回值是把球归类到篮子里的方式的数量。如果不能完全存放到篮子中,无法划分,返回0。
篮子互不同,所有的球相同。因此,如果2个球放到2个篮子里,你可以采用3种方式,即(0, 2), (1, 1), 或 (2, 0)Method:
方法:
countWaysParameters:
参数:
int, int, intReturns:
返回值:
intMethod signature:
方法原型:
int countWays(int baskets, int capacity, int balls)
(be sure your method is public)
注意方法是publicConstraints
条件:
-
baskets will be between 1 and 5, inclusive.
篮子数量在1到5之间
-
capacity will be between 1 and 20, inclusive.
容量在1到20之间
-
balls will be between 1 and 100, inclusive.
球在1到100之间
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.
只有1个球,我们必须3选一
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).
可以把两个球放在相同的篮子里(有三种方式),或者两个篮子里各放1球(有三种方法)
3)????
1
5
10
Returns: 0
We have more balls than our basket can hold.
球的数量大于篮子的容量
4)????
4
5
10
Returns: 146This 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.

2.代码示例

import java.util.*;
public class FillBaskets {int count=0;int baskets,capacity,balls;int[] bas;public int countWays(int baskets, int capacity, int balls) {if (baskets*capacity<balls)return 0;this.baskets=baskets;this.balls=balls;if (balls<capacity)capacity=balls;this.capacity=capacity;bas=new int[baskets];//从第0个篮子开始放putBalls(0);return count;}//void putBalls(int n) {//检测是不是到达最后篮子if (n==baskets) {if (getSumBalls()==balls)count++;return;}//给第n个篮子放球,并且从0->capacity//一次轮循的放入for (int i=0;i<=capacity;i++) {bas[n]=i;putBalls(n+1);}}//得到当前篮子里面所有的球int getSumBalls() {int sum=0;for (int i=0;i<baskets;i++)sum+=bas[i];return sum;}
}
public class BasketTest {public static boolean Test1() {FillBaskets fb=new FillBaskets();int baskets=2;int capacity=20;int balls=2;int key=3;int result=fb.countWays(baskets,capacity,balls);System.out.println (result);return result==key;}public static boolean Test2() {FillBaskets fb=new FillBaskets();int baskets=3;int capacity=20;int balls=1;int key=3;int result=fb.countWays(baskets,capacity,balls);System.out.println (result);return result==key;}public static boolean Test3() {FillBaskets fb=new FillBaskets();int baskets=3;int capacity=20;int balls=2;int key=6;int result=fb.countWays(baskets,capacity,balls);System.out.println (result);return result==key;}public static boolean Test4() {FillBaskets fb=new FillBaskets();int baskets=1;int capacity=5;int balls=10;int key=0;int result=fb.countWays(baskets,capacity,balls);System.out.println (result);return result==key;}public static boolean Test5() {FillBaskets fb=new FillBaskets();int baskets=4;int capacity=5;int balls=10;int key=146;int result=fb.countWays(baskets,capacity,balls);System.out.println (result);return result==key;}public static void main(String[] args) {if (Test1()) {System.out.println ("Test1 Ok");}if (Test2()) {System.out.println ("Test2 Ok");}if (Test3()) {System.out.println ("Test3 Ok");}if (Test4()) {System.out.println ("Test4 Ok");}if (Test5()) {System.out.println ("Test5 Ok");}}
}

经典算法题-球和篮子相关推荐

  1. java经典100例算法题_10道java经典算法题,每一题都能帮你提升java水平!

    JAVA经典算法题 [程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第四个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析:兔子 ...

  2. 经典算法题每日演练——第六题 协同推荐SlopeOne 算法

    原文:经典算法题每日演练--第六题 协同推荐SlopeOne 算法 相信大家对如下的Category都很熟悉,很多网站都有类似如下的功能,"商品推荐","猜你喜欢&quo ...

  3. 经典算法题每日演练——第二十二题 奇偶排序

    原文:经典算法题每日演练--第二十二题 奇偶排序 这个专题因为各种原因好久没有继续下去了,MM吧...你懂的,嘿嘿,不过还得继续写下去,好长时间不写,有些东西有点生疏了, 这篇就从简单一点的一个&qu ...

  4. 经典算法题每日演练——第十九题 双端队列

    经典算法题每日演练--第十九题 双端队列 原文:经典算法题每日演练--第十九题 双端队列 话说大学的时候老师说妹子比工作重要~,工作可以再换,妹子这个...所以...这两个月也就一直忙着Fall in ...

  5. 经典算法题每日演练——第十题 树状数组

    原文:经典算法题每日演练--第十题 树状数组 有一种数据结构是神奇的,神秘的,它展现了位运算与数组结合的神奇魅力,太牛逼的,它就是树状数组,这种数据结构不是神人是发现不了的. 一:概序 假如我现在有个 ...

  6. 经典算法题每日演练——第一题 百钱买百鸡

    经典算法题每日演练--第一题 百钱买百鸡 原文:经典算法题每日演练--第一题 百钱买百鸡 百钱买百鸡的问题算是一套非常经典的不定方程的问题,题目很简单:公鸡5文钱一只,母鸡3文钱一只,小鸡3只一文钱, ...

  7. python全排序算法题_Python的100道经典算法题(1)

    按照c语言的100道经典算法题,自己原创写的,就得是自己的练习题了 [程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? 程序分析:可填在百位.十位.个位的数 ...

  8. 经典算法题每日演练——第七题 KMP算法

    原文:经典算法题每日演练--第七题 KMP算法 在大学的时候,应该在数据结构里面都看过kmp算法吧,不知道有多少老师对该算法是一笔带过的,至少我们以前是的, 确实kmp算法还是有点饶人的,如果说红黑树 ...

  9. 每日经典算法题(十六) 九九乘法表

    每日经典算法题(十六) 九九乘法表 九九乘法表:Multiplication Table 99 题目 输出 9 * 9 口诀 程序分析 分行与列考虑,共9行9列,i 控制行,j 控制列 思路 非常经典 ...

最新文章

  1. 【2021斯坦福新书】统计学思维,300页pdf
  2. 哪个学校计算机系学大物,计算机系各专业专业及名校介绍
  3. 【PC工具】更新chrome谷歌浏览器最新离线安装版各种版本,最好用的浏览器没有之一...
  4. 重新抛出异常与异常链
  5. python 图像处理_Python中的十大图像处理工具
  6. PTA6、输出10个不重复的英文字母 (10 分)
  7. cdev_init函数
  8. sitecore系统教程之体验编辑器
  9. 自然语言处理中的词性标注全称
  10. matlab做聚类分析(简单的直接用clusterdata)
  11. android源代码居中字体,一篇文章了解移动端文本垂直居中
  12. 【职场篇】游戏开发社招求职面试指南③——面试总结
  13. 青岛啤酒12星座铝瓶星耀公开 啤酒也有自己的朋友圈
  14. 公共基础知识:什么是“商品”
  15. 郑州大学计算机在职博士招生简章,郑州大学医学在职博士招生简章
  16. ALC、AGC、DRC、EQ对比
  17. 我国软件昂首阔步向前走
  18. 飘易关键字排名批量查询工具分享!
  19. Socialbook告诉你这才是KOL营销的终极秘诀
  20. 基于java的校园二手网站的设计与实现

热门文章

  1. 42个机器学习练手项目
  2. sougou linux 无法切换中英文,Ubuntu 16.04安装GoLand后不能切换到搜狗输入法
  3. PySpark 累加器使用及自定义累加器
  4. Zxing.jar下载
  5. java opencv 更换图片背景色(基于ROI)
  6. DC/DCT/DCG 差别和联系
  7. 巨大的市场潜力,细数2019国内云计算新排名
  8. 关于ECharts怎么隐藏掉坐标轴
  9. 灰色关联度矩阵模型及其MATLAB实现
  10. 变形金刚2中的各个角色,及车型(带图)第一篇。