Files
2022-01-23 00:08:20 +09:00

1.9 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
59713bd26bdeb8a594fb9413 硬貨を数える 5 302238 count-the-coins

--description--

米国 通貨には4種類の共通硬貨があります。

  • 25セント硬貨
  • 10セント硬貨
  • 5セント硬貨
  • 1セント硬貨

15セントを両替するには6つの組み合わせがあります。

  • 10セント硬貨1枚と5セント硬貨1枚
  • 10セント硬貨1枚と1セント硬貨5枚
  • 5セント硬貨3枚
  • 5セント硬貨2枚と1セント硬貨5枚
  • 5セント硬貨1枚と1セント硬貨10枚
  • 1セント硬貨15枚

--instructions--

米国の1セント硬貨である 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];
}