题解 | #称砝码#
称砝码
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))