题解 | #数组分组#

数组分组

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 () {
    const n = parseInt(await readline());
    const nums = (await readline()).split(" ").map(Number);
    let three = 0, five = 0, rest = [];
    for(const num of nums){
        if(num % 5 === 0) {
            five += num;
        }else if(num % 3 === 0){
            three += num;
        }else{
            rest.push(num);
        }
    }
    let target = Math.abs(three-five),res = false;
    // console.log(target,five,three,rest)
    const dfs = (i) => {
        if(i < 0) return res ||= target===0;
        //不选
        target += rest[i];
        dfs(i-1);
        target -= rest[i];
        //选
        target -= rest[i];
        dfs(i-1);
        target += rest[i];//还原现场
    }
    dfs(rest.length-1);
    console.log(res);
}()

华为OD,我昵称“od400” 文章被收录于专栏

华为OD机试刷题历程,因涉及华为权益,牛客不允许更新了。我昵称“od400”,某博客继续更新。

全部评论

相关推荐

点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务