题解 | 称砝码
称砝码
https://www.nowcoder.com/practice/f9a4c19050fc477e9e27eb75f3bfd49c
n = int(input()) g = list(map(int, input().split(" "))) num = list(map(int, input().split(" "))) g_type = {0} for i in range(n): current_weight = g[i] # 当前选取砝码重量 current_num = num[i] # 当前重量的砝码数量 temp_set = set() # 临时集合存可能产生的新值 for w in g_type: # 便利已经入集合的重量,在其基础上增加看是否会出现新值 for k in range(current_num + 1): # 遍历当前重量的砝码数量 if ( w + k * current_weight not in g_type ): # 也可写作new_wight = w+k*current_weight temp_set.add( w + k * current_weight ) # 后直接用集合各值唯一的特性temp_set.add()无需if判定 g_type.update(temp_set) # 将总的新值更新到总集合中 print(len(g_type))