题解 | #记票统计#

记票统计

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 () {
    let length = await readline();
    let candidates_array = await readline();
    candidates_array = candidates_array.split(" ");
    let votes_length = await readline();
    let votes_array = await readline();
    votes_array = votes_array.split(" ");
    let map = new Map();
    let res = 0;
    for (e of candidates_array) {
        map.set(e, 0);
    }
    for (e of votes_array) {
        if (map.has(e)) {
            map.set(e, map.get(e) + 1);
        } else {
            res += 1;
        }
    }
    let output = "";
    values = map.values();
    keys = map.keys();
    for (i = 0; i < map.size; i++) {
        value = values.next().value;
        key = keys.next().value;
        if (i < map.size - 1) {
            output += `${key} : ${value}\n`;
        } else {
            output += `${key} : ${value}`;
        }
    }
    // for (let [key, value] of map) {
    //     output += `${key} : ${value}\n`;
    // }
    console.log(output);
    console.log("Invalid : " + res);
})();

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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