题解 | #记票统计#

记票统计

https://www.nowcoder.com/practice/3350d379a5d44054b219de7af6708894

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
    let arrinput = [];
    while ((line = await readline())) {
        arrinput.push(line);
    }
    let people = arrinput[1].split(" ");
    let vote = arrinput[3].split(" ");
    // 定义num统计所有的合法票数
    let num = 0;
    // 定义result数组用于存放“人员 : 票数”结果
    let result = [];
    people.forEach((item, index) => {
        // 定义count用于统计每一个人的合法得票数
        let count = 0;
        // 循环统计每一个人共有多少张得票
        vote.forEach((x, i) => {
            if (item == x) {
                count++;
            }
        });
        // 每一个人循环结束后,将其票数加到总得合法票数中
        num += count;
        // 存放结果
        result.push(item + " " + ":" + " " + count);
    });
    // 循环存放人员得票之后,存放不合规的票数
    result.push("Invalid : " + (vote.length - num));
    // 依次输出结果
    for (let item of result) {
        console.log(item);
    }
})();

全部评论

相关推荐

我是没经验的毕业生,这啥情况啊会不会是hr在刷kpi
JamesGosli...:字节boss属于是群发了,我都快入职字节了,其他部门还在和我boss打招呼
点赞 评论 收藏
分享
06-07 19:59
门头沟学院 C++
补药卡我啊😭:都快15年前的了还在11新特性
你的简历改到第几版了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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