- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 10、30 和 50 的硬币。但我只想使用 M
硬币来获得给定的 sum
。
我有这段代码(来自 this 作为引用)它只是找到所有可能的方法来获得总和,而不适用仅使用 M
硬币的条件。
static long countWays(int coins[], int n, int sum)
{
// Time complexity of this function: O(n*sum)
// Space Complexity of this function: O(sum)
// table[i] will be storing the number of solutions
// for value i. We need sum+1 rows as the table is
// constructed in bottom up manner using the base
// case (sum = 0)
long[] table = new long[sum + 1];
// Initialize all table values as 0
Arrays.fill(table, 0);
// Base case (If given value is 0)
table[0] = 1;
// Pick all coins one by one and update the table[]
// values after the index greater than or equal to
// the value of the picked coin
for (int i = 0; i < n; i++)
for (int j = coins[i]; j <= sum; j++)
table[j] += table[j - coins[i]];
return table[sum];
}
// Driver Function to test above function
public static void main(String args[])
{
int coins[] = { 10, 30, 50 };
int n = coins.length;
int sum = 80;
System.out.println(countWays(coins, n, sum));
}
我们如何为这个问题添加该条件,或者是否有任何替代方法。
例如:
M=4 and sum = 80
输出 2.
解释:
case 1 : 10*2 + 30*2 = 80 ( used 4 coins i.e. M coins)
case 2 : 10*3 + 50*1 = 80 ( used 4 coins i.e. M coins)
约束:
M reaches up to 5000
sum reaches up to 250000
最佳答案
思考这个问题的一种方法是count in a different base system .您使用独特硬币的数量作为基础。因此,对于 10、30 和 50 的示例,底数为 3。
现在您需要该基本系统中的数字具有正确的位数,在您的示例中为 4。由于每个数字只能是 3 个基数(0、1 或 2)中的一个,因此可能的总数是 3 的 4 次方,即 81。
因此我们可以用十进制数从 0 到 80,然后使用 stacked repeated division 将该十进制数转换为以 3 为基数的四位数。 .
这是以 3 为基数的四位数字的样子:
0 in base 3: [0, 0, 0, 0]
1 in base 3: [0, 0, 0, 1]
2 in base 3: [0, 0, 0, 2]
3 in base 3: [0, 0, 1, 0]
4 in base 3: [0, 0, 1, 1]
5 in base 3: [0, 0, 1, 2]
6 in base 3: [0, 0, 2, 0]
7 in base 3: [0, 0, 2, 1]
8 in base 3: [0, 0, 2, 2]
9 in base 3: [0, 1, 0, 0]
10 in base 3: [0, 1, 0, 1]
11 in base 3: [0, 1, 0, 2]
12 in base 3: [0, 1, 1, 0]
13 in base 3: [0, 1, 1, 1]
14 in base 3: [0, 1, 1, 2]
15 in base 3: [0, 1, 2, 0]
16 in base 3: [0, 1, 2, 1]
17 in base 3: [0, 1, 2, 2]
18 in base 3: [0, 2, 0, 0]
19 in base 3: [0, 2, 0, 1]
20 in base 3: [0, 2, 0, 2]
21 in base 3: [0, 2, 1, 0]
22 in base 3: [0, 2, 1, 1]
23 in base 3: [0, 2, 1, 2]
24 in base 3: [0, 2, 2, 0]
25 in base 3: [0, 2, 2, 1]
26 in base 3: [0, 2, 2, 2]
27 in base 3: [1, 0, 0, 0]
28 in base 3: [1, 0, 0, 1]
29 in base 3: [1, 0, 0, 2]
30 in base 3: [1, 0, 1, 0]
31 in base 3: [1, 0, 1, 1]
32 in base 3: [1, 0, 1, 2]
33 in base 3: [1, 0, 2, 0]
34 in base 3: [1, 0, 2, 1]
35 in base 3: [1, 0, 2, 2]
36 in base 3: [1, 1, 0, 0]
37 in base 3: [1, 1, 0, 1]
38 in base 3: [1, 1, 0, 2]
39 in base 3: [1, 1, 1, 0]
40 in base 3: [1, 1, 1, 1]
41 in base 3: [1, 1, 1, 2]
42 in base 3: [1, 1, 2, 0]
43 in base 3: [1, 1, 2, 1]
44 in base 3: [1, 1, 2, 2]
45 in base 3: [1, 2, 0, 0]
46 in base 3: [1, 2, 0, 1]
47 in base 3: [1, 2, 0, 2]
48 in base 3: [1, 2, 1, 0]
49 in base 3: [1, 2, 1, 1]
50 in base 3: [1, 2, 1, 2]
51 in base 3: [1, 2, 2, 0]
52 in base 3: [1, 2, 2, 1]
53 in base 3: [1, 2, 2, 2]
54 in base 3: [2, 0, 0, 0]
55 in base 3: [2, 0, 0, 1]
56 in base 3: [2, 0, 0, 2]
57 in base 3: [2, 0, 1, 0]
58 in base 3: [2, 0, 1, 1]
59 in base 3: [2, 0, 1, 2]
60 in base 3: [2, 0, 2, 0]
61 in base 3: [2, 0, 2, 1]
62 in base 3: [2, 0, 2, 2]
63 in base 3: [2, 1, 0, 0]
64 in base 3: [2, 1, 0, 1]
65 in base 3: [2, 1, 0, 2]
66 in base 3: [2, 1, 1, 0]
67 in base 3: [2, 1, 1, 1]
68 in base 3: [2, 1, 1, 2]
69 in base 3: [2, 1, 2, 0]
70 in base 3: [2, 1, 2, 1]
71 in base 3: [2, 1, 2, 2]
72 in base 3: [2, 2, 0, 0]
73 in base 3: [2, 2, 0, 1]
74 in base 3: [2, 2, 0, 2]
75 in base 3: [2, 2, 1, 0]
76 in base 3: [2, 2, 1, 1]
77 in base 3: [2, 2, 1, 2]
78 in base 3: [2, 2, 2, 0]
79 in base 3: [2, 2, 2, 1]
80 in base 3: [2, 2, 2, 2]
每个结果数组中的整数(以 3 为基数)表示原始硬币值中的哪个硬币应该放在那个位置(0 = 10、1 = 30、2 = 50)。
例如,十进制数 61 在基数 3 中为 2021:
61 in base 3: [2, 0, 2, 1]
该数字的最终硬币组合为:
50, 10, 50, 30
下面是生成上述 3 进制数字列表的代码:
import java.util.Arrays;
class Main {
public static void main(String[] args) {
int sum = 80;
int numCoins = 4;
int[] coins = new int[]{10, 30, 50};
int base = coins.length;
int combos = (int)Math.pow(base, numCoins);
int[][] combinations = new int[combos][];
for(int d=0; d<combos; d++) {
combinations[d] = convertToBase(d, base, numCoins);
System.out.println(d + " in base " + base + ": " + Arrays.toString(combinations[d]));
}
}
public static int[] convertToBase(int decimalNumber, int base, int numDigits) {
int[] digits = new int[numDigits];
int index = digits.length - 1;
int quotient = decimalNumber;
while (quotient > 0) {
digits[index] = quotient % base;
index--;
quotient = quotient / base;
}
return digits;
}
}
现在您已经有了四种硬币的所有可能组合,您需要将每个组合的值相加,看看它们加起来是否为 80。
这是一个新的 main() 函数:
public static void main(String[] args) {
int sum = 80;
int numCoins = 4;
int[] coins = new int[]{10, 30, 50};
int base = coins.length;
int combos = (int)Math.pow(base, numCoins);
int[][] combinations = new int[combos][];
for(int d=0; d<combos; d++) {
combinations[d] = convertToBase(d, base, numCoins);
String combo = "";
int curSum = 0;
for(int coinChoice : combinations[d]) {
combo = combo + coins[coinChoice] + " ";
curSum = curSum + coins[coinChoice];
}
if (curSum == sum) {
System.out.println("Coins: " + combo + " = " + curSum);
}
}
}
生成以下输出:
Coins: 10 10 10 50 = 80
Coins: 10 10 30 30 = 80
Coins: 10 10 50 10 = 80
Coins: 10 30 10 30 = 80
Coins: 10 30 30 10 = 80
Coins: 10 50 10 10 = 80
Coins: 30 10 10 30 = 80
Coins: 30 10 30 10 = 80
Coins: 30 30 10 10 = 80
Coins: 50 10 10 10 = 80
请注意这里有重复,因为可以将相同面额的硬币组合放入四个插槽的不同位置。
如果你想去掉重复项,你可以对结果组合进行排序并将它们添加到 Hashmap 中。如果它们尚不存在(添加 import java.util.HashMap;
):
public static void main(String[] args) {
int sum = 80;
int numCoins = 4;
int[] coins = new int[]{10, 30, 50};
int base = coins.length;
int combos = (int)Math.pow(base, numCoins);
int[][] combinations = new int[combos][];
HashMap<String, String> uniqueCombos = new HashMap<String, String>();
for(int d=0; d<combos; d++) {
combinations[d] = convertToBase(d, base, numCoins);
String combo = "";
int curSum = 0;
for(int coinChoice : combinations[d]) {
combo = combo + coins[coinChoice] + " ";
curSum = curSum + coins[coinChoice];
}
if (curSum == sum) {
Arrays.sort(combinations[d]);
String key = Arrays.toString(combinations[d]);
if (!uniqueCombos.containsKey(key)) {
uniqueCombos.put(key, combo);
System.out.println("Coins: " + combo + " = " + curSum);
}
}
}
}
现在我们在输出中只得到两个独特的组合:
Coins: 10 10 10 50 = 80
Coins: 10 10 30 30 = 80
这是整个程序的最终版本:
import java.util.Arrays;
import java.util.HashMap;
class Main {
public static void main(String[] args) {
int sum = 80;
int numCoins = 4;
int[] coins = new int[]{10, 30, 50};
int base = coins.length;
int combos = (int)Math.pow(base, numCoins);
int[][] combinations = new int[combos][];
HashMap<String, String> uniqueCombos = new HashMap<String, String>();
for(int d=0; d<combos; d++) {
combinations[d] = convertToBase(d, base, numCoins);
String combo = "";
int curSum = 0;
for(int coinChoice : combinations[d]) {
combo = combo + coins[coinChoice] + " ";
curSum = curSum + coins[coinChoice];
}
if (curSum == sum) {
Arrays.sort(combinations[d]);
String key = Arrays.toString(combinations[d]);
if (!uniqueCombos.containsKey(key)) {
uniqueCombos.put(key, combo);
System.out.println("Coins: " + combo + " = " + curSum);
}
}
}
}
public static int[] convertToBase(int decimalNumber, int base, int numDigits) {
int[] digits = new int[numDigits];
int index = digits.length - 1;
int quotient = decimalNumber;
while (quotient > 0) {
digits[index] = quotient % base;
index--;
quotient = quotient / base;
}
return digits;
}
}
关于java - 条件为恰好使用 m 个硬币的硬币找零问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74434050/
在过去的几个月里,我一直在研究 Haskell,我遇到了一个我不太确定如何处理的单子(monad)的情况。 我有一个 a -> m a 类型的值第二个类型为 m (a -> a)我需要对它们进行组合,
仿函数有 (a -> b) -> m a -> m b 应用程序有 f (a -> b) -> f a -> f b Monad 有 m a -> (a -> m b) -> m b 但是,是否有扩展
我是 Haskell 的新手,我想知道是否有比 Hoogle 更好的方法来确定一个库功能是否重复? 举个例子:我有很多函数f :: Monad a => a -> m a我想链接在一起,比如 f123
将存储在一系列列表中的 m、m、n 维数组组合成一个 m、m、n 维数组的方法是什么? 示例: 这是三个包含 m,m,n 维数组的列表: list1 <- array (1, dim = c(5, 5
有没有办法写一个函数f::(a -> b -> ... -> t) -> (Monad m => m a -> m b -> ... -> m t ),基本上是 liftMn 对于任何 n? (编辑:
我有一个像这样的 pandas 数据框: df = pd.DataFrame({'A':[1,3,2,9],'B':[2,1,2,7],'C':[7,2,4,6],'D':[8,1,6,4]},ind
这个问题来自文章“Trivial Monad”,地址:http://blog.sigfpe.com/2007/04/trivial-monad.html 。提供的答案是 h x y = x >>= (
所以>>= :: m a -> (a -> m b) -> m b和>> :: m a -> m b -> m b . 而 f b -> f a . 但我想要一些能m a -> (a -> m b)
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 3 年前。 Improve
当我安装 rakudo来源: $ git clone git@github.com:rakudo/rakudo.git $ cd rakudo $ perl Configure.pl --gen-mo
我正在尝试通过查看一些练习来提高我的 Idris 技能 Software Foundations (最初是为 Coq 设计的,但我希望对 Idris 的翻译不会太糟糕)。我在使用 "Exercise:
我想知道以下是否可行。 与服务器交换密码时,应保护密码。因此,用户可以使用生成的 key kUser 来加密密码。 Encrypt(m, kUser) 生成加密消息 eU(m)。现在用户将此信息发送到
这两个表之间存在什么样的关系(1:1、1:m、m:m,等等)? CREATE TABLE IF NOT EXISTS `my_product` ( `id` int(11) NOT NULL au
有人可以解释类型的含义以及如何实现吗? class Foldable f where foldMap :: (Monoid m) => (a -> m) -> f a -> m 基于 https:
例如,在 MVC 应用程序中,我可以使用 Html 助手来创建这样的标签: @Html.LabelFor(m => m.ProductName) 我没有在任何地方声明变量“m”,但 IDE 会自动找出
更新:澄清、更明确的重点和缩短的示例: 我可以避免 M op+(M&&,M&&) 过载吗?假设,我想很好地处理 RValues?我想其他三个重载是必需的。 我首先使用 (&&,&&) 重载的原因: 通
假设我有一个函数,它接受两个向量并返回一个整数,例如一个向量中也存在另一个向量中的元素数量。喜欢: f m [,1] [,2] [,3] [1,] "c" "i" "c" [2,] "
我想将字符串(字幕)转换为: 585 00:59:59,237 --> 01:00:01,105 - It's all right. - He saw us! 586 01:00:01,139 -->
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及预
是否可以将 Linux 中的大文件将 d.m.Y h:m:s 转换为 Y-d-m h:m:s? 示例数据 "30.07.2016 00:00:00",DN123,PAPN,PAPN,TEST,9189
我是一名优秀的程序员,十分优秀!