JavaScript中的贪心算法
以下是我需要咨询以寻求帮助的问题: 写一个贪婪的算法,以尽可能少的硬币进行兑换 使用贪婪算法。您将获得一个硬币值数组和 金额:JavaScript中的贪心算法,javascript,algorithm,Javascript,Algorithm,以下是我需要咨询以寻求帮助的问题: 写一个贪婪的算法,以尽可能少的硬币进行兑换 使用贪婪算法。您将获得一个硬币值数组和 金额:computeChange(硬币,金额)。返回一个带有 每枚硬币的计数 例如:computeChange([50,25,10,5,1],137)应该返回 数组[2,1,1,0,2]表示每枚硬币的数量:2 50美分,1个25美分,1个10美分,没有5美分 2便士(1美分),总计137美分 从computeChange返回的数组长度应与 第一个论点(硬币)。假设硬币包含 按降
computeChange(硬币,金额)
。返回一个带有
每枚硬币的计数
例如:computeChange([50,25,10,5,1],137)
应该返回
数组[2,1,1,0,2]
表示每枚硬币的数量:2
50美分,1个25美分,1个10美分,没有5美分
2便士(1美分),总计137美分
从computeChange返回的数组长度应与
第一个论点(硬币)。假设硬币包含
按降序排列的不同硬币类型
贪婪算法说你反复寻找最大的
少于或等于剩余金额的硬币,则
从剩余的钱中减去那枚硬币。当剩下的
金额达到零(或更少),返回使用的硬币计数。(本
算法并不总是最优的。)
您可以更改变量COINS
,该变量给出
您可以使用不同的硬币进行兑换,以及金额
,这是
要进行的更改的总值。更改这些值可能是错误的
对调试程序很有用
这是我的代码,我做了,但它没有显示36美分的标准变化。有人能帮我吗?多谢各位
<html>
<head>
<title>The Greedy Algorithm</title>
<script>
// ======== Here is the problem to be solved: ========
COINS = [50, 25, 10, 5, 1];
AMOUNT = 137
coincount = [0,0,0,0,0];
// ======== Here is where your solution begins: ========
// define the function named computeChange here:
function computeChange(coins, amount) {
var i = 0; var creminder = AMOUNT; var ccoin;
while( i < COINS.length )
{
while ( COINS[i] <= creminder )
{
creminder = creminder - COINS[i];
ccoin = coincount [i] ;
ccoin += 1;
coincount [i] = ccoin ;
}
i++;
}
return coincount;
}
// ===================================================================
// ======== Everything below here simply displays your output ========
// ======== Do NOT change anything below this line ===================
// ===================================================================
function rightJustify(s, w) {
// return a string of width w with s in the rightmost characters and
// at least one space on the left. For simplicity, assume w < 20.
var slen = s.length;
var blanks = " "
return blanks.substr(0, Math.min(20, Math.max(1, w - slen))) + s;
}
function makeChange() {
// compute change as an array: each element of change tells
// how many of the corresponding value in COINS to give. The
// total value should equal AMOUNT.
var change = computeChange(COINS, AMOUNT);
// now format the results. Output should look like:
// NUMBER VALUE
// 1 50
// 0 25
// 1 10
// 1 5
// 3 1
// TOTAL AMOUNT: 68 (total is correct)
//
// First, we'll do some type checking in case change is not of the
// expected type.
change = [].concat(change); // force whatever it is to be an array
// it should be an array of numbers, so let's check
for (i = 0; i < change.length; i++) {
if (typeof(change[i]) != 'number') {
return "Error: the function computeChange did not return " +
"an array of numbers.";
}
}
if (change.length > COINS.length) {
return "Error: the function computeChange returned an array " +
"longer than the length (" + COINS.length + ") of COINS.";
}
if (change.length < COINS.length) {
return "Error: the function computeChange returned an array " +
"shorter than the length (" + COINS.length + ") of COINS.";
}
var output = "<pre>NUMBER VALUE\n"
var sum = 0;
for (i = 0; i < change.length; i++) {
sum += change[i] * COINS[i];
var n = change[i].toString();
var a = COINS[i].toString();
output += rightJustify(n, 4) + rightJustify(a, 9) + "\n";
}
output += "TOTAL AMOUNT: " + sum + " (total is ";
output += (sum == AMOUNT ? "correct" :
"incorrect, should be " + AMOUNT) + ")\n";
return output;
}
function runSolution()
{
parent.console.log('loaded, calling runSolution()\n');
parent.console.log('answer: ' + document.getElementById('answer').toString());
document.getElementById('answer').innerHTML = makeChange();
}
</script>
</head>
<body>
<!-- the output is displayed using HTML -->
<!-- the ? will be replaced with the answer -->
<div id = "answer">?</div></p>
<br>
<script>runSolution();</script>
</body>
</html>
贪婪算法
//=========以下是需要解决的问题:========
硬币=[50,25,10,5,1];
金额=137
硬币计数=[0,0,0,0,0];
//======以下是解决方案的起点:========
//在此处定义名为computeChange的函数:
函数computeChange(硬币、金额){
var i=0;var creminder=金额;var ccoin;
而(ifunction computeChange(coins, amount) {
// Create a array that is used to return the final result, instead of the global one.
var coincount = [];
// use the given `amount` to set `creminder ` rather than `AMOUNT` which may not be accessible if your code is called otherplace rather than here.
var i = 0; var creminder = amount; var ccoin;
while( i < coins.length )
{
// Lazily init the used coin for coin type i to 0.
coincount[i] = 0;
while ( coins[i] <= creminder )
{
creminder = creminder - coins[i];
ccoin = coincount[i];
ccoin += 1;
coincount[i] = ccoin;
}
i++;
}
return coincount;
}
var总和=0;
对于(i=0;i运行解决方案(); 想法: 阅读回复后,首先想到的是,这可能用于我们在这里没有看到的其他代码,因此我们需要使函数足以通过输入解决问题,而不是使用
全局值,如金额
,硬币
和硬币计数
,而是使用硬币
等给定的参数damount
,并返回一个自行创建的coincount
我会直接用代码中的注释来解释这一点
function computeChange(changeArray, amount) {
const result = [];
for (let i = 0; i < changeArray.length; i++) {
let changeAmount = Math.floor(amount / changeArray[i]);
amount -= (changeArray[i] * changeAmount);
result.push(changeAmount);
}
return result;
}
computeChange([50, 25, 10, 5, 1], 137); // [2, 1, 1, 0, 2]
函数计算范围(硬币、金额){
//创建用于返回最终结果而不是全局结果的数组。
var coincount=[];
//使用给定的'amount'设置'creminder',而不是'amount',如果您的代码在其他地方而不是在这里调用,则可能无法访问该设置。
var i=0;var creminder=金额;var ccoin;
而(i
- 您可以获得
硬币
和金额
的值。原始函数访问
硬币
和金额
即使存在该值的本地副本
creminder
是不必要的,因为您有金额
ccoin
是不必要的,因为您可以直接从金额中减去所选硬币的价值
var硬币=[50,25,10,5,1],
金额=36;//137
函数computeChange(硬币、金额){
var i=0,
coincount=coins.map(函数(){return 0;});//返回一个数组,coins的每个元素返回零
而(i 虽然上面的答案非常正确,但我认为人们也可以用不同的方式来思考这个特定问题的解决方案
以computeChange([50,25,10,5,1],137)
为例,可以使用单个循环来获得所需的解决方案
function cc(c, a) {
for (var ra=[],i=0,r=a; i<c.length; ra[i] = (r/c[i])|0, r -= ra[i]*c[i], i++);
return ra;
}
function cc2(c, a) {
return c.map((c, i) => { var t = (a/c)|0; a -= c*t; return t; })
}
cc([50, 25, 10, 5, 1], 137); // [2, 1, 1, 0, 2]
cc2([50, 25, 10, 5, 1], 137); // [2, 1, 1, 0, 2]
函数计算范围(更改数组、金额){
常量结果=[];
for(设i=0;i
功能cc(c,a){
对于(var ra=[],i=0,r=a;i{var t=(a/c)|0;a-=c*t;返回t;})
}
抄送([50,25,10,5,1],137);//[2,1,1,0,2]
cc2([50,25,10,5,1],137);//[2,1,1,0,2]
什么意思它没有显示36美分的标准零钱?我的意思是我的代码有什么问题,或者我需要做什么来显示36美分的标准零钱?因为我一直在互联网上尝试和搜索,大多数示例都是用VB和其他编程