- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下午好,
我目前正在开发一个程序,该程序应该使用回溯算法来找到达到特定金额所需的硬币总数的解决方案。
程序的基本布局是这样的
User is prompted for an amount (ie. 123)
int amount = input.nextInt();
User is prompted for number of coins (ie. 6)
int numCoins = input.nextInt();
User is prompted for coin values (ie. 2, 4, 32, 51, 82)
int[] array = new array[] {2, 4, 32, 51, 82};
根据这些信息,我将开发一种回溯算法来输出解决方案。
我试图查找回溯信息,但没有真正的效果。这一切对我来说似乎都很不清楚我到底应该从算法开始。
感谢任何帮助。
编辑
这是我目前一直在做的事情......目前还没有工作
public class coins
{
public static int[] coinValues;
public static int currentAmount = 0;
public static void main(String[] args)
{
ArrayList<Integer> temp = new ArrayList<>();
Scanner input = new Scanner(System.in);
System.out.println("Please enter the amount: ");
int amount = input.nextInt();
System.out.println("Please enter the number of coins: ");
int numCoins = input.nextInt();
coinValues = new int[numCoins];
for (int i = 0; i < numCoins; i++)
{
System.out.println("Please enter coin value of " + i + " :");
int value = input.nextInt();
coinValues[i] = value;
}
for (int i = 0; i < coinValues.length; i++)
{
System.out.print("Coin Value: " + i + " " + coinValues[i] + "\n");
}
tryThis(temp, amount);
for (int i = 0; i < temp.size(); i++)
{
System.out.println(temp.get(i) + " " + " ");
}
}
public static ArrayList<Integer> tryThis(ArrayList<Integer> list, int amount)
{
if (isValid(list, amount) == false)
{
while (amount > currentAmount && (amount > 0))
{
for (int i = 0; i < coinValues.length; i++)
{
for (int k = coinValues.length - 1; k > 0; k--)
{
list.add(coinValues[k]);
int currentCoin = list.get(i);
if (amount > currentAmount)
{
amount = amount - currentCoin;
System.out.println("Amount: " + amount);
}
tryThis(list, amount);
}
}
}
}
return new ArrayList<>();
}
public static boolean isValid(ArrayList list, int amount)
{
boolean keepGoing = true;
if (amount > currentAmount)
{
return keepGoing = false;
}
else
{
return keepGoing;
}
}
}
问候,迈克
最佳答案
你的基本算法将如下所示:
For a given amount
For each coin type
Add the coin to a set
If the set exceeds the amount, discard that set.
If the set contains more coins than it should, discard the set.
If the set equals the amount, add that set to the set of valid possibilities.
Otherwise run the algorithm on each set you've created.
通过回溯,您通常会保留算法每次迭代中剩余的分配量(因此称为“回溯”,因为您试图找到越来越小的量的解决方案)。例如,假设我正在使用 10 角硬币、5 分镍币和 1 便士寻找 0.07 美元:
I start with empty sets.
I add a dime to one set.
I subtract '10' from my amount.
This is a negative number, so I discard that set: it is invalid.
I add a nickel to another (empty) set.
I subtract '5' from my amount.
This equals 2; so I'll have to keep working on this set.
Now I'm working with sets that already include one nickel.
I add a dime to one set.
I subtract '10' from my amount.
This is a negative number, so I discard that set: it is invalid.
I repeat this with a nickel; I discard this possibility because (2 - 5) is also negative.
I repeat this with a penny; this is valid but I still have 1 left.
I repeat this whole process again with a starting set of one nickel and one penny,
again discarding a dime and nickel,
and finally adding a penny to reach an amount of 0: this is a valid set.
Now I go back to empty sets and repeat starting with a nickel, then pennies.
请注意,该算法应产生多个结果:
[nickel, penny, penny]
[penny, nickel, penny]
[penny, penny, nickel]
[penny, penny, penny, penny, penny, penny, penny]
函数式语言自然适合这种递归工作,但您可以用任何语言复制此算法。
这将是实现代码的示例,不包括重复的优雅修剪:
import java.util.*;
public class Backtrack {
private static List<List<Integer>> findSets(int amount, List<Integer> coinTypes, int numberOfCoins) {
List<List<Integer>> results = new ArrayList<List<Integer>>();
for (Integer coin : coinTypes) {
List<Integer> result = new ArrayList<Integer>();
result.add(coin);
int currentAmount = amount - coin;
if (currentAmount >=0) { //only continue if we haven't overshot
if (currentAmount == 0) {
results.add(result);//this is a valid solution, add it to result set
} else {//still some value to make up
if ((numberOfCoins - 1) > 0){//otherwise we shouldn't recurse
List<List<Integer>> recurseResults = findSets(currentAmount, coinTypes, (numberOfCoins - 1));
for (List<Integer> recurseResult : recurseResults) {//Have to add this layer's coin in to each result
recurseResult.add(coin);
}
results.addAll(recurseResults);
}
}
}
}
return results;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int amount = 7;
List<Integer> coinTypes = new ArrayList<Integer>();
coinTypes.add(Integer.valueOf(1));
coinTypes.add(Integer.valueOf(5));
coinTypes.add(Integer.valueOf(10));
int numberOfCoins = 3;
List<List<Integer>> results = findSets(amount, coinTypes, numberOfCoins);
System.out.println("Number of results: " + results.size());
for (List<Integer> result : results) {
System.out.print("Result: ");
for (Integer coin: result) {
System.out.println(result.toString() + ",");
}
}
}
}
关于Java BackTracking 与硬币,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15530863/
我目前正在做一个项目,试图开发一种用于 P2P 能源交易的货币和模型,其中每产生一千瓦时的可再生能源就会向该生产商类型转换一枚硬币。我的问题是关于销毁智能合约中的代币。 所有与我的项目类似的当前实现都
在没有Maps帮助的情况下通过Memoization解决问题,由于读取文件的方法,我得到了TLE,根据我的说法,这不应该是这种情况。可能的原因是什么? 这是给出 AC - http://ideone.
考虑下面这段伪代码,其中d是面额值数组,k是面额数,n是要进行更改的金额。 Change(d; k; n) 1 C[0] 我真的不明白这部分,你为什么要用它,谁能给我解释一下! 最佳答案 为了回答
我正在尝试在我的网站上实现 Coin Slider (http://workshop.rs/2010/04/coin-slider-image-slider-with-unique-effects/)
我有使用硬币 slider 的画廊 var $jq = jQuery.noConflict(); $jq(window).load(function() { var imhei
我使用了从该站点提取的硬币 slider http://workshop.rs/projects/coin-slider/ .它现在自动滚动并仅在悬停时显示上一个和下一个。我需要禁用自动滚动并正常显示
我的问题是一道CodeFu练习题(2012 round 2 problem 3)。它基本上归结为将整数数组分成两个(几乎)相等的两半并返回两者之间可能的最小差异。我在下面包含了问题描述。如评论中所述,
我们的老师要求我们制作一 jar 硬币,用来计算我们有多少便士、一毛钱等,然后给出总金额。 这是他希望我们使用的模板 https://online.pcc.edu/content/enforced/7
我正在尝试使用币安币 future 的 api 下载 BTC/USD 永续 future 的历史价格数据,具体来说,我想使用 this endpoint .但是,我找不到必须为 BTC/USD 指定的
我上周刚开始学习计算机科学,我们得到了一个名为“硬币”的工作表,其中我必须找出一组硬币中有多少个 25 美分、10 美分、5 美分和 10 便士。我遇到了很多麻烦,并收到了该错误。这是我的代码 pac
我正在构建一些使用消耗性硬币的测验。我使用 NSUserDefault 来保存设备上的硬币及其工作。我还在 qiuz 中使用 CloudKit 处理数据。 不麻烦的是,如果用户切换设备如何恢复硬币?有
我是一名优秀的程序员,十分优秀!