题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
思路:输入以元组形式存入列表,将元组元素添加或追加到字典,最后字典排序输出键值
n = int(input())
d = {}
l = [tuple(map(int,input().split())) for _ in range(n)] # 输入以元组形式存入列表, for elem in l:
if elem[0] in d.keys():
d[elem[0]] += elem[1] # 追加
else:
d[elem[0]] = elem[1] # 新增
for k,v in sorted(d.items()): # 排序输出键值
print(k,v)