Huawei Machine Test - Weighing Weight

describe

There are n kinds of weights with unequal weights, namely m1, m2, m3...mn; the
corresponding quantities of each weight are x1, x2, x3...xn. Now use these weights to weigh the object (put it on the same side), and ask how many different weights can be weighed.

Note:

Weighed weights include 0

Data range: each set of input data satisfies 1 \le n \le 10 \1≤n≤10 , 1 \le m_i \le 2000 \1≤m i
​≤2000
,
1 \le x_i \le 10 \
1≤xi ​≤10

enter description

For each set of test data:
the first row: n — the number of weights (range [1,10])
the second row: m1 m2 m3 … mn — the weight of each weight (range [1,2000]
) Three lines: x1 x2 x3 … xn — the number corresponding to each weight (range [1,10])

output description

The number of different weights that can be weighed with a given weight

code analysis

n = int(input())
m = input().split()
x = input().split()
# 排列组合
# 所有砝码的重量列表,组合重量
fam = []
for i in range(n):
    fam.extend([int(m[i])] * int(x[i]))
res = {
    
    0}  # 结果集
res_1 = {
    
    }  # 中间结果集
# 使用集合取并集的方法比较方便,每加一块,相当于对已经有的重量都加一遍,然后去重
for i in fam:
    # 每增加一块砝码,所有组合都加一遍
    res_1 = {
    
    i + j for j in res}
    # 取并集去重
    res = res.union(res_1)
print(len(res))

Guess you like

Origin blog.csdn.net/summerriver1/article/details/128224871