题解 | #数组分组#
数组分组
https://www.nowcoder.com/practice/9af744a3517440508dbeb297020aca86
// 剩余的数字只有两种选择要么加给3的阵营要么加到5的阵营 // 必须全部的数字得到分配,如果分配完毕的话还是没有结果就是false // 用递归去穷举 const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; const tempArr = []; void (async function () { // Write your code here while ((line = await readline())) { tempArr.push(line); } let arr3 = 0; let arr5 = 0; const other = []; tempArr[1] .split(" ") .map(Number) .forEach((e) => { if (e % 3 === 0 && e % 5 !== 0) { arr3 += e; } else if (e % 3 !== 0 && e % 5 === 0) { arr5 += e; } else { other.push(e); } }); function canEqual(arr3, arr5, other, index) { if (index === other.length && arr3 === arr5) return true; if (index === other.length && arr3 !== arr5) return false; if (index <= other.length - 1) return ( canEqual(arr3 + other[index], arr5, other, index + 1) || canEqual(arr3, arr5 + other[index], other, index + 1) ); return false; } console.log(canEqual(arr3, arr5, other, 0)); })();