题解 | #称砝码#

称砝码

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))

全部评论

相关推荐

一tiao酸菜鱼:秋招还没正式开始呢,就准备有结果了。。。。?
点赞 评论 收藏
分享
06-02 15:17
门头沟学院 Java
心爱的idea:怎么会呢 应该是打招呼有问题 问就说实习6个月全国可飞随时到岗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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