题解 | #数组分组#

数组分组

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 combine = (fiveSum, threeSum, others) => {
        if (others.length === 0) {
            if (fiveSum === threeSum) return true;
            else return false;
        }
        if (combine(fiveSum + others[0], threeSum, others.slice(1)))
            return true;
        if (combine(fiveSum, threeSum + others[0], others.slice(1)))
            return true;
    };

    let index = 0;
    let arr;
    while ((line = await readline())) {
        if (index === 0) index++;
        else arr = line.split(" ").map(Number);
    }
    let others = [];
    let fiveSum = 0;
    let threeSum = 0;
    for (let a of arr) {
        if (a % 5 === 0) fiveSum += a;
        else if (a % 3 === 0) threeSum += a;
        else others.push(a);
    }
    if (combine(fiveSum, threeSum, others)) console.log("true");
    else console.log("false");
})();

全部评论

相关推荐

头像
昨天 13:58
门头沟学院 Java
烤点老白薯:他已经混出来了 所以可以站在一个高度上PUA我们 我们却还在底层挣扎
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务