题解 | #称砝码#

称砝码

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

# 输入砝码重量,砝码重量范围,砝码数量
types = int(input().strip())
weights = list(map(int, input().split()))
quantities = list(map(int, input().split()))

def weight_calculate(n, weights, quantities):
    # 跟踪所有可能的重量组合,从0开始
    possible_weights = {0}
    
    # 遍历所有可能的重量组合
    for i in range(n):
        current_weight = weights[i]
        current_quantity = quantities[i]
        # 初始化一个set用来储存每一轮遍历中发现的新的重量。
        new_weights = set()
        
        # 对于每一个储存在可能重量中的元素进行处理
        for existing_weight in possible_weights:
            # 从0到当前的砝码数量开始进行current_weight * quantity
            for q in range(1, current_quantity + 1):
                new_weight = existing_weight + q * current_weight
                new_weights.add(new_weight)
        
        # 使用每一轮新的重量来更新可能的重量组合
        possible_weights.update(new_weights)
    
    # 返回砝码数量
    return len(possible_weights)

# 打印结果
print(weight_calculate(types, weights, quantities))

全部评论

相关推荐

见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
zhch7:建议9✌️把学历加黑加粗,如果实在offer可能是觉得佬不会去
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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