华为机试 - 合并表记录JS解答
合并表记录
http://www.nowcoder.com/questionTerminal/de044e89123f4a7482bd2b214a685201
看了一圈这个应该是js最简洁的写法,简单易懂,用obj存储和计算还不需要排序,直接使用Object.entries输出完事
var readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); const obj = {} rl.on('line', function(line){ var tokens = line.split(' '); if(tokens[1]){ if(obj[tokens[0]]){ obj[tokens[0]] += Number(tokens[1]) } else { obj[tokens[0]] = Number(tokens[1]) } } }); rl.on('close',()=>{ for (const [key, value] of Object.entries(obj)) { console.log(`${key} ${value}`); } })