2.1 KiB
2.1 KiB
title, id, localeTitle, challengeType
title | id | localeTitle | challengeType |
---|---|---|---|
Count the coins | 59713bd26bdeb8a594fb9413 | 59713bd26bdeb8a594fb9413 | 5 |
Description
Hay cuatro tipos de monedas comunes en moneda estadounidense :
trimestres (25 centavos) monedas de diez centavos (10 centavos) centavos (5 centavos) y centavos (1 centavo)Hay seis formas de hacer cambio por 15 centavos:
Un centavo y un centavo Un centavo y 5 centavos 3 centavos 2 centavos y 5 centavos Un centavo y 10 centavos 15 centavos Tarea:Implemente una función para determinar cuántas maneras hay de hacer un cambio por un dólar usando estas monedas comunes. (1 dólar = 100 centavos).
Referencia: un algoritmo de MIT Press .Instructions
Tests
tests:
- text: <code>countCoins</code> es una función.
testString: 'assert(typeof countCoins === "function", "<code>countCoins</code> is a function.");'
- text: <code>countCoints()</code> debe devolver 242.
testString: 'assert.equal(countCoins(), 242, "<code>countCoints()</code> should return 242.");'
Challenge Seed
function countCoins () {
// Good luck!
return true;
}
Solution
function countCoins () {
let t = 100;
const operands = [1, 5, 10, 25];
const targetsLength = t + 1;
const operandsLength = operands.length;
t = [1];
for (let a = 0; a < operandsLength; a++) {
for (let b = 1; b < targetsLength; b++) {
// initialise undefined target
t[b] = t[b] ? t[b] : 0;
// accumulate target + operand ways
t[b] += (b < operands[a]) ? 0 : t[b - operands[a]];
}
}
return t[targetsLength - 1];
}