题解 | #数组分组#
数组分组
https://www.nowcoder.com/practice/9af744a3517440508dbeb297020aca86
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void (async function () { // Write your code here const inputs = []; while ((line = await readline())) { inputs.push(line); } let res = false; const getSum = (arr) => { return arr.reduce((acc, item) => acc + item, 0); }; const loop = (arr1, arr2, inputs) => { if (inputs.length === 0) { if (getSum(arr1) === getSum(arr2)) { res = true; return; } return; } let val = inputs.pop(); // console.log(arr1, arr2, val); if (val % 5 === 0) { loop([...arr1, val], arr2, [...inputs]); return; } else if (val % 3 === 0) { loop(arr1, [...arr2, val], [...inputs]); return; } // console.log(arr1, arr2, val, "a"); loop([...arr1, val], arr2, [...inputs]); loop(arr1, [...arr2, val], [...inputs]); }; loop([], [], inputs[1].split(" ").map(Number)); console.log(res); })();