题解 | 数对计数

数对计数

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

import sys
from collections import Counter
N,C = map(int,input().split())
lst = list(map(int,input().split()))
#num_count为每个数出现的次数,key为num val为count
num_count=Counter(lst)
#计算满足条件的数对
result = 0
for num in num_count:
    target = num+C

    if target in num_count:
        #对满足条件的数对排列组合
        result+=num_count[num]*num_count[target]
print(result)

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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