【发布时间】:2015-06-27 16:21:26
【问题描述】:
这是我需要咨询的问题:
编写一个贪心算法,用尽可能少的硬币找零 使用贪心算法。给你一个硬币值数组和 金额:
computeChange(coins, amount)。返回一个数组 每个硬币的计数。例如:
computeChange([50, 25, 10, 5, 1], 137)应该返回 数组[2, 1, 1, 0, 2],表示每个硬币有多少:2 50 美分,1 夸特(25 美分),1 角钱(10 美分),无镍币(5 美分)和 2 便士(1 美分),加起来是 137 美分。您从 computeChange 返回的数组的长度应与 第一个论点(硬币)。假设硬币包含 不同的硬币类型按降序排列。
贪心算法说你反复寻找最大的 硬币小于或等于剩余金额,则 从剩余金额中减去该硬币。当剩下的 金额达到零(或更少),返回使用的硬币计数。 (这 算法并不总是最优的。)
您可以更改变量
COINS,它给出了 你可以用不同的硬币来找零,还有AMOUNT,这是 要进行的更改的总价值。更改这些值可能是 对调试程序很有用。
这是我的代码,但它没有显示 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>
【问题讨论】:
-
什么意思它没有显示 36 美分的标准变化。 ?
-
我的意思是我的代码有什么问题,或者我需要做什么才能显示 36 美分硬币的标准零钱?因为我一直在尝试和在互联网上搜索大多数示例都是用 VB 和其他编程语言编写的,而不是 javascript。
-
您的意思是,您希望问题的值从 137 变为 36?
标签: javascript algorithm