题解 | #数组分组#

数组分组

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));
})();

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-08 12:05
俺不中了,BOSS遇到了一个hr,我觉得我咨询的问题都很正常吧,然后直接就被拒绝了???
恶龙战士:你问的太多了,要不就整理成一段话直接问他,一个一个问不太好
点赞 评论 收藏
分享
05-14 20:34
门头沟学院 Java
窝补药贝八股:管他们,乱说,反正又不去,直接说680
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-07 13:46
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务