题解 | #称砝码#

称砝码

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

#思路一致,原始测量数组只有0,对选定1个砝码与数组中全部值相加,然后去重,不断循环。
#本网站的未知bug:获取数据时如果写input().split(' ')在本地运行不会报错,但在本网站可能会报错。
#故而改成input().split()较为稳妥。
'''写法1: 
import sys
while 1:
    try:
        n = int(input())
        m = list(map(int, input().split()))
        x = list(map(int, input().split()))
    except:
        break
    else:
        mx = []
        for i in range(n):
            for j in range(x[i]):
                mx.append(m[i])
        weight = {0,}
        for i in mx:
            for j in list(weight):
                weight.add(i+j)
        print(len(weight))
'''
#写法2.
import sys
n = int(input())
m = list(map(int,input().split()))
x = list(map(int,input().split()))
weight = set([0])
for mi,xi in zip(m,x):
    for i in range(xi):
        weight |= set([x+mi for x in weight])
print(len(weight))

全部评论

相关推荐

Lyxiho:浙江大学 加大加粗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务