google中国编程挑战赛题目FillBaskets
2005-12-14 13:59
405 查看
题目:
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.
源代码:
/**
*
* @author greentea
* Email: greenteanet@gmail.com
* Copyright: Copyright(c) 2005
*/
public class FillBaskets {
private static int count = 0;
public static int countWays(int baskets, int capacity, int balls) {
count =0;
if (baskets * capacity < balls)
return count;
count(baskets,capacity,balls);
return count;
}
public static void count(int baskets, int capacity, int balls){
if (balls == 0){
count++;
}
for (int i = 1; i <= baskets; i++) {
for (int j = 1; j <= balls; j++) {
if (j <= capacity) {
count(baskets - i, capacity, balls - j);
}
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(countWays(1, 5, 10));
System.out.println(countWays(4, 5, 10));
System.out.println(countWays(3, 10, 2));
System.out.println(countWays(3, 20, 1));
System.out.println(countWays(2, 20, 2));
}
}
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.
源代码:
/**
*
* @author greentea
* Email: greenteanet@gmail.com
* Copyright: Copyright(c) 2005
*/
public class FillBaskets {
private static int count = 0;
public static int countWays(int baskets, int capacity, int balls) {
count =0;
if (baskets * capacity < balls)
return count;
count(baskets,capacity,balls);
return count;
}
public static void count(int baskets, int capacity, int balls){
if (balls == 0){
count++;
}
for (int i = 1; i <= baskets; i++) {
for (int j = 1; j <= balls; j++) {
if (j <= capacity) {
count(baskets - i, capacity, balls - j);
}
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(countWays(1, 5, 10));
System.out.println(countWays(4, 5, 10));
System.out.println(countWays(3, 10, 2));
System.out.println(countWays(3, 20, 1));
System.out.println(countWays(2, 20, 2));
}
}
相关文章推荐
- Google中国编程挑战赛-题目解答
- google中国编程挑战赛题目
- Google中国编程挑战赛-题目解答
- Google中国编程挑战赛第一轮Room18第二题
- 中国选手包揽前两名! Google全球编程挑战赛落幕
- Google(谷歌)全球编程挑战赛中国获奖选手(花絮)
- [转]google编程挑战赛揭幕 中国选手有望免费赴美比赛
- python代码(2)---google中国编程挑战赛入围赛真题HardDuplicateRemover(1000分)
- Google™ Code Jam - 中国编程挑战赛
- 浅尝 Google™ Code Jam - 中国编程挑战赛
- Google CodeJam 中国编程挑战赛拉开帷幕
- Google中国编程挑战赛资格赛
- Google中国编程挑战赛第一轮
- Google™ Code Jam - 中国编程挑战赛模拟题二
- google中国编程挑战赛资格赛真题 -- SkipStones
- Google中国编程挑战赛一窥
- Google™ Code Jam - 中国编程挑战赛模拟题三
- Google中国编程挑战赛专访李开复: 做终身程序员是一件好事情 2005.11.23
- 参加Google™ Code Jam - 中国编程挑战赛(2)
- google中国编程挑战赛资格赛真题 -- PlayCards