第一行输入一个整数
代表砝码的个数。
第二行输入
个整数
代表每种砝码的重量。
第三行输入
个整数
代表每种砝码的数量。
输出一个整数,代表利用给定的砝码可以称出的不同的重量数。
2 1 2 2 1
5
在这个样例中,有
个重量为
的砝码,
个重量为
的砝码。称重方式如下:
不放砝码,称重重量为
;
放
个重量为
的砝码,称重重量为
;
放
个重量为
的砝码,称重重量为
;
放
个重量为
的砝码、
个重量为
的砝码,称重重量为
;
放
个重量为
的砝码、
个重量为
的砝码,称重重量为
。
因此,能称出的不同重量有
种,分别是
。
已老实,下述代码内存和时间不符合规范T T
n = int(input()) weight = list(map(int, input().split())) numebr = list(map(int, input().split())) ipt_dict = dict() for w, num in zip(weight, numebr): ipt_dict[w] = num res = set() init_dict = dict() for w in weight: init_dict[w] = 0 stack: list[dict] = [] stack.append(init_dict) while stack: ## add to set top_dict = stack.pop() keys = list(top_dict.keys()) # print(top_dict) res.add(sum([top_dict[key] * key for key in keys])) ## 砝码数量 for key in weight: if top_dict[key] < ipt_dict[key]: new_dict = top_dict.copy() new_dict[key] += 1 stack.append(new_dict) print(len(res))y
fm_type = int(input()) fm_num = [int(i) for i in input().split()] fm_weight = [int(i) for i in input().split()] list_val = [ fm_num[i] for i in range(fm_type) for j in range(fm_weight[i])] list_val_tmp = {0,} for i in list_val: for j in list(list_val_tmp): list_val_tmp.add(i+j) print(len(set(list_val_tmp)))
import itertools kind = int(input()) wei = input() count = input() dic = [] for i in range(kind): dic.append((wei.split()[i],count.split()[i])) w1 = [0] * (kind + 1) for i in dic: w2 = [] for j in range(0,int(i[1])+1): z = j * int(i[0]) w2.append(z) w1 = [x + y for x, y in itertools.product(w1, w2)] p=[] for i in w1: if i not in p: p.append(i) print(len(p))
kindNum=int(input()) line2=list(map(int,input().split())) line3=list(map(int,input().split())) famaList=list(zip(line2,line3)) #第一列是砝码重量,第二列是数量 ##迭代函数 def get_sum2(): result_arr=[] result_arr.append(0) for p in range(kindNum): result_arr=sorted(set(result_arr)) this_result_num=len(result_arr) for q in range(1,famaList[p][1]+1): for r in range(this_result_num): result_arr.append(result_arr[r]+famaList[p][0]*q) return result_arr print(len(sorted(set(get_sum2()))))
num_types = int(input()) weights = map(int, input().split()) nums = map(int, input().split()) possible_weights = {0} for weight, num in zip(weights, nums): possible_weights = possible_weights | { possible_weight + weight * add_num for possible_weight in possible_weights for add_num in range(0, num + 1) } print(len(possible_weights))
该题实质是完全背包问题的一个变种,如果使用排列组合来找所有子集的方法时间复杂度高容易超时。
使用动态规划算法解决:
1. 定义状态:使用一个集合 possible_weights 来记录可能称出的所有重量。
2. 迭代每种砝码:对于每个砝码 m_i,在其对应的数量 x_i 之间进行迭代。 使用一个临时集合 new_weights 来保存当前迭代过程中产生的新重量。 对于现有的每个可能重量 w,添加 j * m_i(j 在 1 到 x_i 范围内),并将其添加到 new_weights 中。 最后,将 new_weights 中的所有重量添加到 possible_weights 中。
3. 计算所有可能重量:在完成所有砝码迭代后,possible_weights 应包含所有可能称出的重量。
4. 返回结果:possible_weights 的长度即为可能称出的不同重量数。 import sys
def count_possible_weights(n, weights, quantities):
# 初始化可能称出的重量
possible_weights = {0} # 初始化为空重
# 迭代每种砝码
for i in range(n):
current_weight = weights[i]
current_quantity = quantities[i]
# 新的可能重量集合
new_weights = set()
# 计算所有可能的重量
for w in possible_weights:
for j in range(1, current_quantity + 1):
new_weight = w + j * current_weight
new_weights.add(new_weight)
# 合并到可能重量集合中
possible_weights.update(new_weights)
# 返回可能称出的不同重量数
return len(possible_weights)
lines = []
for line in sys.stdin:
line = line.strip()
if line == "":
break
lines.append([int(i) for i in line.split(" ")])
for i in range(len(lines) // 3):
n = lines[i][0]
weights = lines[i + 1]
quantities = lines[i + 2]
print(count_possible_weights(n, weights, quantities))
def count_measurable_weights(n, weights, quantities): max_weight = sum(weight * quantity for weight, quantity in zip(weights, quantities)) measurable_weights = set() measurable_weights.add(0) for weight, quantity in zip(weights, quantities): new_weights = set() for i in range(1, quantity + 1): for measurable_weight in measurable_weights: new_weights.add(measurable_weight + weight * i) measurable_weights.update(new_weights) return len(measurable_weights) # Read input n = int(input()) weights = list(map(int, input().split())) quantities = list(map(int, input().split())) # Calculate and print the result result = count_measurable_weights(n, weights, quantities) print(result)1. 首先是函数定义:
# 参考了大佬的代码,集合NB n = int(input()) fama_weight = list(map(int,input().split())) fama_num = list(map(int,input().split())) sum = {0} for i in range(n): jh = [] for j in range(1,fama_num[i]+1): jh.append(fama_weight[i]*j) bf = sum.copy() for k in bf: for l in jh: sum.add(k+l) print(len(sum))
def getWeightNum(m_arr,x_arr,pre_weight_map = {0:True},x = 0): weight_map = {} for pre_weight in pre_weight_map.keys(): for i in range(x_arr[x]+1): weight_map[pre_weight + i*m_arr[x]] = True if(x == len(x_arr)-1): return len(weight_map) return getWeightNum(m_arr,x_arr,weight_map,x+1) weight_num = int(input()) m_arr = list(map(int,input().split())) x_arr = list(map(int,input().split())) weight_map = {} print(getWeightNum(m_arr,x_arr))
n = int(input().strip()) kind_ls = list(map(int, input().strip().split())) num_ls = list(map(int, input().strip().split())) # 砝码列表 砝码种类、各类砝码数量 -> 砝码列表 fama_ls = [] for i in range(n): for _ in range(num_ls[i]): fama_ls.append(kind_ls[i]) weights = {0} # 每次取出一个砝码 与先前所有情形组合 for fama in fama_ls: weights |= {w + fama for w in weights} print(len(weights))
n = int(input()) m = list(map(int,input().split())) x = list(map(int,input().split())) mx = []#只放一个砝码时有多少种情况 for i in range(n): mx.extend([m[i]]*x[i]) weights = {0}#每次只加一个砝码上去记录下此时的重量 for i in mx: #每次放入一个i和重量记录表各个值相加得到新的砝码组合重量,集合自动去重 weights = weights.union({i+j for j in weights}) print(len(weights))
from collections import Counter n=int(input()) weight=list(map(int,input().strip().split(' '))) num=list(map(int,input().strip().split(' '))) w={0,} c=Counter(dict(zip(weight,num))) for i in c.elements(): w.update(set(i+j for j in w)) print(len(w))
n=int(input()) m=input().split() x=input().split() weight=[0] for i in range(n): m[i]=int(m[i]) x[i]=int(x[i]) for i in range(n): tmp_list=[m[i]*j for j in range(x[i]+1)] weight=list(set(a+b for a in tmp_list for b in weight)) print(len(weight))