题解 | #称砝码# set.add()

称砝码

https://www.nowcoder.com/practice/f9a4c19050fc477e9e27eb75f3bfd49c

n=int(input())
m=list(map(int,input().split()))
x=list(map(int,input().split()))

m1=[]
for i in range(n):
    for j in range(x[i]):
        m1.append(m[i])
#print(m1)

weight=0
weight_rec={0} # 集合,自动去重,记录重量,包括0
for i in m1:
    for j in list(weight_rec):
        weight_rec.add(i+j)


print(len(weight_rec))


每增加一个重量的砝码,前期的重量组合都要更新(新砝码与每个组合重量相加),用集合去重。

全部评论

相关推荐

06-04 09:27
门头沟学院 Java
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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