题解 | #合并表记录#

合并表记录

https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201?tpId=37&tags=&title=&difficulty=0&judgeStatus=0&rp=1&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D37

let readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let num = 0;
let obj = {};
rl.on('line', line => {
    if(num === 0) {
        num = parseInt(line);
    } else  {
        let [k, v] = line.split(' ');
        if (k in obj) {
            obj[k] += Number(v);
        }
        else {
            obj[k] = Number(v);
        }
    }
})

rl.on('close', () => {
    Object.keys(obj).map((it) => {
       console.log(it, obj[it]);
    })
})

全部评论

相关推荐

爱看电影的杨桃allin春招:我感觉你在炫耀
点赞 评论 收藏
分享
评论
1
收藏
分享
牛客网
牛客企业服务