题解 | #称砝码#

称砝码

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

# 获得砝码种数n
# 获得砝码重量weigh
# 获得每个砝码个数nums=[]
# 获得砝码乘个数的重量
# 初始化set(),temp,加入砝码i,遍历
n=int(input())
weight=list(map(int,input().split()))
nums=list(map(int,input().split()))
famas=[]
path={0,}
for i in range(n):
    for j in range(nums[i]):
        famas.append(weight[i])
        # 记录出现次数
for i in famas:
    temp=list(path)
    for j in temp:
        path.add(i+j)
print(len(path))

famas记录每个砝码出现次数,往集合里面加入每个砝码,具体步骤是:遍历temp,每一个元素加入当前砝码重量,在加入path里面。更新path集合;得到答案就是砝码范围

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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