题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
const n = readline()
const workingMap = new Map()
//录入map
while(pair = readline()){
const key = parseInt(pair.split(' ')[0])
const val = parseInt(pair.split(' ')[1])
if(workingMap.has(key)){
const oldVal = workingMap.get(key)
const newVal = oldVal + val
workingMap.set(key, newVal)
}else{
workingMap.set(key, val)
}
}
//获得顺序key数组
const keyIter = workingMap.keys()
const keyArr = Array.from(keyIter)
keyArr.sort((a, b) => a < b? -1:1)
//print
for(let i = 0; i < keyArr.length; i++){
console.log(keyArr[i], workingMap.get(keyArr[i]))
}
const workingMap = new Map()
//录入map
while(pair = readline()){
const key = parseInt(pair.split(' ')[0])
const val = parseInt(pair.split(' ')[1])
if(workingMap.has(key)){
const oldVal = workingMap.get(key)
const newVal = oldVal + val
workingMap.set(key, newVal)
}else{
workingMap.set(key, val)
}
}
//获得顺序key数组
const keyIter = workingMap.keys()
const keyArr = Array.from(keyIter)
keyArr.sort((a, b) => a < b? -1:1)
for(let i = 0; i < keyArr.length; i++){
console.log(keyArr[i], workingMap.get(keyArr[i]))
}