题解 | #HJ8 合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
# 方法1
n = int(input())
dic = {}
for _ in range(n):
line = input().split()
index = int(line[0])
value = int(line[1])
if index not in dic:
dic[index] = value
else:
dic[index] += value
for k, v in sorted(dic.items()):
print(k, v)
# 方法2
# 如果dic中存在index,则使用index对应的value值;否则使用设定的默认值0
# dic[index] = dic.get(index, 0) + value
【牛客站内】华为机试题解 文章被收录于专栏
【牛客站内】 分享个人刷题的思路和解法