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

2.6 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
59f40b17e79dbf1ab720ed7a 部署番号 5 302249 department-numbers

--description--

高度に組織化された都市が、各部署に番号を割り当てることにしました。

  • 警察局
  • 公衆衛生局
  • 消防局

各部署は1から7までの数字を持つことができます。

3つの部署番号は (互いに異なる) 一意の数字で、足して12にする必要があります。

警察長官は、奇数が好きではないので、部署に偶数番号が欲しいと考えています。

--instructions--

すべての有効な組み合わせを配列として出力するプログラムを作成します。

[2, 3, 7] [2, 4, 6] [2, 6, 4]
[2, 7, 3] [4, 1, 7] [4, 2, 6]
[4, 3, 5] [4, 5, 3] [4, 6, 2]
[4, 7, 1] [6, 1, 5] [6, 2, 4]
[6, 4, 2] [6, 5, 1]

--hints--

combinations という関数です。

assert(typeof combinations === 'function');

combinations([1, 2, 3], 6) は配列を返します。

assert(Array.isArray(combinations([1, 2, 3], 6)));

combinations([1, 2, 3, 4, 5, 6, 7], 12) は14の配列を返します。

assert(combinations(nums, total).length === len);

combinations([1, 2, 3, 4, 5, 6, 7], 12) はすべての有効な組み合わせを返します。

assert.deepEqual(combinations(nums, total), result);

--seed--

--after-user-code--

const nums = [1, 2, 3, 4, 5, 6, 7];
const total = 12;
const len = 14;
const result = [
  [2, 3, 7],
  [2, 4, 6],
  [2, 6, 4],
  [2, 7, 3],
  [4, 1, 7],
  [4, 2, 6],
  [4, 3, 5],
  [4, 5, 3],
  [4, 6, 2],
  [4, 7, 1],
  [6, 1, 5],
  [6, 2, 4],
  [6, 4, 2],
  [6, 5, 1]
];

--seed-contents--

function combinations(possibleNumbers, total) {

  return true;
}

--solutions--

function combinations(possibleNumbers, total) {
  let firstNumber;
  let secondNumber;
  let thridNumber;
  const allCombinations = [];

  for (let i = 0; i < possibleNumbers.length; i += 1) {
    firstNumber = possibleNumbers[i];

    if (firstNumber % 2 === 0) {
      for (let j = 0; j < possibleNumbers.length; j += 1) {
        secondNumber = possibleNumbers[j];

        if (j !== i && firstNumber + secondNumber <= total) {
          thridNumber = total - firstNumber - secondNumber;

          if (thridNumber !== firstNumber && thridNumber !== secondNumber && possibleNumbers.includes(thridNumber)) {
            allCombinations.push([firstNumber, secondNumber, thridNumber]);
          }
        }
      }
    }
  }
  return allCombinations;
}