题解 | #合并表记录#
合并表记录
http://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
a = int(input())
dic = {}
for i in range(a):
tem = input().split( )
index = int(tem[0])
value = int(tem[1])
if index in dic:##直接判断引索字符串在不在字典里
dic[index] = dic[index] + value
else:
dic[index] = value
# print(sorted(dic.items())) [(0, 3), (1, 2), (3, 4)]#sorted帮你转变成列表
# print(type((dic.items()))) <class 'dict_items'>
# print(dic.items()) dict_items([(0, 3), (1, 2), (3, 4)])
for w in sorted(dic.items()):
print(w[0],w[1])
dic = {}
for i in range(a):
tem = input().split( )
index = int(tem[0])
value = int(tem[1])
if index in dic:##直接判断引索字符串在不在字典里
dic[index] = dic[index] + value
else:
dic[index] = value
# print(sorted(dic.items())) [(0, 3), (1, 2), (3, 4)]#sorted帮你转变成列表
# print(type((dic.items()))) <class 'dict_items'>
# print(dic.items()) dict_items([(0, 3), (1, 2), (3, 4)])
for w in sorted(dic.items()):
print(w[0],w[1])
【牛客站内】华为机试题—简单 文章被收录于专栏
【牛客站内】华为机试题练习记录