题解 | 数对计数

数对计数

https://www.nowcoder.com/practice/7d05171e7e0e4c6086be233769e01d94

n, c = map(int,input().split())
nums = list(map(int,input().split()))

counts = {}
for num in nums:#统计输入数据中,各数据的个数,存储到字典中
    counts[num] = counts.get(num,0)+1

result = 0
for key, value in counts.items():#遍历字典,查看字典中,是否有能和当前数据匹配的数据
    target = key-c
    if target in counts:
        result += value*counts[target]

print(result)

全部评论

相关推荐

挥毫自在:想白嫖你呢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务