CSP 202006-2 稀疏矩阵 python 模拟

CSP 202006-2 稀疏矩阵 python 模拟

题目描述

在这里插入图片描述

在这里插入图片描述

思路

暴力模拟即可得到答案,我们可以用Python的字典,找到相同的键就将值进行相乘即可

代码

n,a,b = map(int,input().split())

d1,d2 = {
    
    },{
    
    }
for i in range(a):
    index,value = map(int,input().split())
    d1[index] = value

for i in range(b):
    index,value = map(int,input().split())
    d2[index] = value
    
ans = 0
for k,v in d1.items():
    if k in d2:
        ans += v * d2[k]
print(ans)

猜你喜欢

转载自blog.csdn.net/weixin_45508265/article/details/125088961