现有n种砝码,重量互不相等,分别为 m1,m2,m3…mn ;
每种砝码对应的数量为 x1,x2,x3...xn 。现在要用这些砝码去称物体的重量(放在同一侧),问能称出多少种不同的重量。
注:
称重重量包括 0
数据范围:每组输入数据满足 , ,
现有n种砝码,重量互不相等,分别为 m1,m2,m3…mn ;
每种砝码对应的数量为 x1,x2,x3...xn 。现在要用这些砝码去称物体的重量(放在同一侧),问能称出多少种不同的重量。
注:
对于每组测试数据: 第一行:n --- 砝码的种数(范围[1,10]) 第二行:m1 m2 m3 ... mn --- 每种砝码的重量(范围[1,2000]) 第三行:x1 x2 x3 .... xn --- 每种砝码对应的数量(范围[1,10])
利用给定的砝码可以称出的不同的重量数
2 1 2 2 1
5
可以表示出0,1,2,3,4五种重量。
while True: try: n, weights, counts = int(input()), list(map(int, input().split())), list(map(int, input().split())) total = [] for i in range(n): for j in range(counts[i]): total.append(weights[i]) unique_weights = {0} for w in total: temp = [] for uw in unique_weights: temp.append(uw+w) unique_weights = unique_weights | set(temp) print(len(unique_weights)) except: break
while True: try: n = int(input().strip()) # 不同重量的砝码的种数n weights = list(map(int, input().strip().split(' '))) # 每种砝码的具体重量的列表 nums = list(map(int, input().strip().split(' '))) # 每种砝码的个数的列表 combines = [0] * n # 初始化不同重量的列表 for i in range(n): # 单纯地看第i种重量的砝码mi,能形成多少种重量的组合(砝码个数取:0,1,...xi个) combines_i = [weights[i] * num for num in range(0, nums[i]+1)] # 将'刚得到的第i种砝码的重量可能列表'与'原来的重量可能列表'进行组合,生成新的重量可能列表 combines = list(set([a+b for a in combines_i for b in combines])) # combines即为最终的不同重量的列表 print(len(combines)) except: break
while True: try: n, weight, count = int(input()), list(map(int, input().split())), list(map(int, input().split())) tmp = [w for w, c in zip(weight, count) for i in range(c)] res = {0} for i in tmp: res_copy = res.copy() for j in res_copy: res.add(j+i) print(len(res)) except: break
while 1: try: N, weight, num = int(input()), input().split(), input().split() mylist = [] for i in range(len(weight)): for j in range(int(num[i])): mylist.append(int(weight[i])) totalw = [0] t = 0 newlist = [] for k in mylist: newset = set(totalw) for h in newset: totalw.append(h+k) print(len(set(totalw))) except: break惭愧,初始代码因为循环过多无法输出,看了评论区大佬的观点才想明白。。。
while True: try: a = int(input()) weight = list(map(int,input().split())) count = list(map(int,input().split())) fm,temp,ans = [],[],[0] # 将所有砝码放入列表 for i in range(a): for j in range(count[i]): fm.append(weight[i]) # 称重 for i in fm: temp = set(ans) for j in temp: ans.append(j+i) # 去重 print(len(set(ans))) except: break
while True: try: n = int(input()) wei = list(map(int, input().split())) num = list(map(int, input().split())) diffwei = set() diffwei.add(0) for i in range(n): for j in range(num[i]): for k in list(diffwei): diffwei.add(k+wei[i]) print(len(diffwei)) except: break用set()直接加入就好啦
/* 利用set自动去重,感觉题目没有说清楚,n表示砝码的种类,不是砝码的数量,另外就是要 加上重量为0的情况……*/ def getWeight(n,weight,num): res = {0} for i in range(n): temp = res.copy() for j in range(1,num[i]+1): for k in temp: res.add(k + weight[i]*j) return len(res) while True: try: n = int(input()) weight = list(map(int,input().split())) num = list(map(int,input().split())) print(getWeight(n,weight,num)) except: break
try:
while True:
N = int(raw_input())
u = map(int,raw_input().split())
v = map(int,raw_input().split())
buf = []
out = [0]
for i in range(N):
for j in range(1,v[i]+1):
buf.append(u[i])
for m in buf:
outbuf = list(set(out[:]))
for n in outbuf:
out.append( m+n )
print len(set(out))
except:
pass
def solution(w, n): cnt = set( w[0]*i for i in range(n[0]+1)) # 初始set for i in range(1, len(w)): # 遍历砝码重量 tmp = cnt.copy() # copy一份 for j in range(n[i]+1): # 处理砝码对应的数量 for k in tmp: # 当前可以称的重量 tmp_w = w[i]*j + k if tmp_w not in cnt: cnt.add(w[i]*j + k) # set自动去重 return len(cnt) import sys try: while 1: line = sys.stdin.readline().strip() if not line: break w = sys.stdin.readline().strip() w = map(int, w.split()) n = sys.stdin.readline().strip() n = map(int, n.split()) print solution(w, n) except: pass