题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
n = int(input()) dict1 = {} for i in range(n): key, value = map(int, input().split()) if key not in dict1: dict1[key] = value # 如果键不存在于字典中,添加新的键值对 else: dict1[key] += value # 如果键已存在,累加它的值 # 输出所有键值对,键值对按键的顺序排序 for key in sorted(dict1.keys()): print(f"{key} {dict1[key]}")