2.1 KiB
2.1 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
59713bd26bdeb8a594fb9413 | Підрахунок монет | 5 | 302238 | count-the-coins |
--description--
У валюті США існує чотири типи звичайних монет:
- четвертаки (25 центів)
- дайми (10 центів)
- нікелі (5 центів) та
- пенні (1 цент)
Існує шість способів розміняти 15 центів:
- 1 дайм та 1 нікель
- 1 дайм та 5 пенні
- 3 нікелі
- 2 нікелі та 5 пенні
- 1 нікель та 10 пенні
- 15 пенні
--instructions--
Створіть функцію, яка буде визначати, скількома способами можна розміняти введену кількість cents
, що представляє суму американських пенні, використовуючи ці поширені монети.
--hints--
countCoins
має бути функцією.
assert(typeof countCoins === 'function');
countCoins(15)
має повертати 6
.
assert.equal(countCoins(15), 6);
countCoins(85)
має повертати 163
.
assert.equal(countCoins(85), 163);
countCoins(100)
має повертати 242
.
assert.equal(countCoins(100), 242);
--seed--
--seed-contents--
function countCoins(cents) {
return true;
}
--solutions--
function countCoins(cents) {
const operands = [1, 5, 10, 25];
const targetsLength = cents + 1;
const operandsLength = operands.length;
const 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];
}