题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
N = input() ks = [] # vs = [] while 1: try: k, v = input().split(' ') ks.append([int(k), int(v)]) # vs.append(v) except: break sorted_ks = sorted(ks, key=lambda x: x[0]) sorted_ks_end_pad = [sorted_ks[-1][0]+1, 0] # 最后一位插入一个键值与之前都不一样的键值对,因为下面的循环不会打印最后一个键值,不同键值是为了不仅如此循环中的第一个分支,导致无法打印 sorted_ks.append(sorted_ks_end_pad) pre_ele = sorted_ks[0] for i, ele in enumerate(sorted_ks[1:]): if ele[0] == pre_ele[0]: pre_ele[1] += ele[1] else: print(pre_ele[0], pre_ele[1]) pre_ele = ele