题解 | #数组分组#

数组分组

https://www.nowcoder.com/practice/9af744a3517440508dbeb297020aca86

const readline = require("readline");

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});
let isIndex = true;
rl.on("line", function (line) {
    if (isIndex) {
        isIndex = false;
    } else {
        const arr = line.split(" ");
        const three = arr.filter((a) => a % 3 === 0 && a % 5 !== 0);
        const five = arr.filter((a) => a % 5 === 0);
        const others = arr.filter((a) => a % 3 !== 0 && a % 5 !== 0);

        const threes = three.reduce((acc, cur) => acc + Number(cur), 0);
        const fives = five.reduce((acc, cur) => acc + Number(cur), 0);
        function getRes(sum1, sum2, index) {
            if (others.length === index) {
                return sum1 === sum2;
            }
            const num = Number(others[index]);
            return (
                getRes(sum1 + num, sum2, index + 1) ||
                getRes(sum1, sum2 + num, index + 1)
            );
        }
        console.log(getRes(threes, fives, 0));
    }
});

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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