总和为S的最小硬币数量
给定 N 个硬币的列表,它们的值 (V1, V2, ... , VN) 以及总和 S。找到总和为 S 的硬币的最小数量(我们可以使用一种类型的硬币数量为我们想要),或者报告不可能以总和为 S 的方式选择硬币。
我试图理解动态规划,但还没有弄清楚。我不明白给定的解释,所以也许你可以给我一些如何编写这个任务的提示?没有代码,只是我应该从哪里开始的想法。
谢谢。
Given a list of N coins, their values (V1, V2, ... , VN), and the total sum S. Find the minimum number of coins the sum of which is S (we can use as many coins of one type as we want), or report that it's not possible to select coins in such a way that they sum up to S.
I try to understand dynamic programming, haven't figured it out. I don't understand the given explanation, so maybe you can throw me a few hints how to program this task? No code, just ideas where I should start.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
这个问题的准确答案在这里得到了很好的解释。
http://www.topcoder.com/tc?module=Static& ;d1=教程&d2=dynProg
The precise answer to this problem is well explained here.
http://www.topcoder.com/tc?module=Static&d1=tutorials&d2=dynProg
这是一个经典的背包问题,请查看此处以获取更多信息:维基百科背包问题
您还应该查看一些排序,特别是从最大到最小的值排序。
This is a classic Knapsack problem, take a look here for some more information: Wikipedia Knapsack Problem
You should also look at some sorting, specifically sorting from Largest to Smallest values.
正如已经指出的,动态规划最适合解决这个问题。我为此编写了一个Python程序:-
对于输入:
12
2 3 5
输出为:
[0, -1, 1, 1, 2, 1, 2, 2, 2, 3, 2, 3, 3]
3
list_index 是所需的总数,list_index 的值是编号。获得该总数所需的硬币。上述输入(得到值 12)的答案是 3(值 5、5、2 的硬币)。
As already pointed out, Dynamic Programming suits best for this problem. I have written a Python program for this:-
For input:
12
2 3 5
The output would be:
[0, -1, 1, 1, 2, 1, 2, 2, 2, 3, 2, 3, 3]
3
The list_index is the total needed and the value at list_index is the no. of coins needed to get that total. The answer for above input(getting a value 12) is 3 ( coins of values 5, 5, 2).
我认为你想要的方法是这样的:
你知道你想要产生一个总和
S
。生产S
的唯一方法是首先生产S-V1
,然后添加价值V1
的硬币;或生产S-V2
,然后添加有价值V2
的硬币;或者...,
T=S-V1
可以从T-V1
或T-V2
生成,或者...反过来 以这种方式退后一步,您可以确定从
V
生成S
的最佳方法(如果有)。I think the approach you want is like this:
You know that you want to produce a sum
S
. The only ways to produceS
are to first produceS-V1
, and then add a coin of valueV1
; or to produceS-V2
and then add a coin of valueV2
; or...In turn,
T=S-V1
is producible fromT-V1
, orT-V2
, or...By stepping back in this way, you can determine the best way, if any, to produce
S
from yourV
s.问题已经得到解答,但我想提供我编写的工作 C 代码,如果它对任何人有帮助的话。
在此处输入代码
代码具有硬编码输入,但这只是为了保持简单。最终的解决方案是数组 min ,其中包含每个总和所需的硬币数量。
Question is already answered but I wanted to provide working C code that I wrote, if it helps anyone.
enter code here
Code has hard coded input, but it is just to keep it simple. Final solution is the array min containing the number of coins needed for each sum.
我不知道动态规划,但这就是我要做的。从零开始,逐步达到
S
。用一枚硬币生产一套,然后用该套生产两枚硬币,依此类推...搜索S
,并忽略所有大于S
的值。对于每个值,请记住所使用的硬币数量。I don't know about dynamic programming but this is how I would do it. Start from zero and work your way to
S
. Produce a set with one coin, then with that set produce a two-coin set, and so on... Search forS
, and ignore all values greater thanS
. For each value remember the number of coins used.很多人已经回答了这个问题。这是使用DP的代码
Lots of people already answered the question. Here is a code that uses DP
主要思想是 - 对于每个硬币 j,value[j] <= i(即总和),我们查看为 i-value[j](比方说 m)总和(之前找到的)找到的最小硬币数量。如果 m+1 小于当前总和 i 已找到的最小硬币数量,则我们更新数组中的硬币数量。
对于前 - sum = 11 n=3 且 value[] = {1,3,5}
以下是我们得到的输出
正如我们可以观察到的 sum i = 3、5、8 和 10,我们通过以下方式在之前的最小值基础上进行了改进 -
因此,对于 sum=11,我们将得到答案 3(5+5+1) 。
这是 C 中的代码。它类似于 topcoder 页面中给出的伪代码,其参考在上面的答案之一中提供。
The main idea is - for each coin j, value[j] <= i (i.e sum) we look at the minimum number of coins found for i-value[j] (let say m) sum (previously found). If m+1 is less than the minimum number of coins already found for current sum i then we update the number of coins in the array.
For ex - sum = 11 n=3 and value[] = {1,3,5}
Following is the output we get
As we can observe for sum i = 3, 5, 8 and 10 we improve upon from our previous minimum in following ways -
So for sum=11 we will get answer as 3(5+5+1).
Here is the code in C. Its similar to pseudocode given in topcoder page whose reference is provided in one of the answers above.
考虑第 i 个硬币。要么包含,要么不包含。如果包含,则价值总和减去币值,使用的币数增加1。如果不包含,则需要类似地探索剩余的币。我们至少采取两种情况。
Consider i-th coin. Either it will be included or not. If it is included, then the value sum is decreased by the coin value and the number of used coins increases by 1. If it is not included, then we need to explore the remaining coins similarly. We take the minimum of two cases.
我知道这是一个老问题,但这是 Java 中的解决方案。
I knew this is a old question, but this is a solution in Java.
对于快速递归解决方案,您可以检查此链接:java解决方案
我正在执行找到完美硬币组合所需的最少步骤。
假设我们有
coins = [20, 15, 7]
和monetaryValue = 37
。我的解决方案将按如下方式工作:For a fast recursive solution, you can check this link: java solution
I am going through the minimum steps required to find the perfect coin combination.
Say we have
coins = [20, 15, 7]
andmonetaryValue = 37
. My solution will work as follow:最少硬币([17,18,2], 100652895656565)
leastCoins([17,18,2], 100652895656565)