#!/usr/bin/env python3 # coding: utf-8 # vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: import itertools as it a = [52.7,8.96] b = [21.44,6.72,5.44,5.12,4.48,3.20,2.24,1.92,1.92,1.92,1.28,1.28,1.00,0.96,0.50,0.32,0.32,0.32,0.32,0.32,0.32,0.32] a = [62.13,26.67,17.76] b = [24.92,5.88,5.04,3.64,3.45,3.36,2.8,2.8,2.52,2.24,2.24,2.24,1.96,1.96,1.8,1.68,1.4,1.4,1.4,1.2,1.2,1.15,1.12,1.12,1.12,1.12,1.12,0.84,0.84,0.84,0.84,0.84,0.84,0.84,0.84,0.84,0.56,0.56,0.56,0.56,0.56,0.56,0.56,0.56,0.56,0.56,0.56,0.56,0.4,0.4,0.4,0.4,0.4,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28,0.28] a = [round(i*100) for i in a] b = [round(i*100) for i in b] def comb(n, lst): new_lst = [i for i in lst if i <= n] if sum(new_lst) == n: return new_lst for i in range(1, len(new_lst)+1): # print(f'C({len(new_lst)}, {i})') for j in it.combinations(new_lst, i): if sum(j) == n: return j def resolve(n, lst): new_lst = [i for i in lst if i <= n] if sum(new_lst) == n: return new_lst uniq_elem = set(new_lst) # count for each element max_repeat = 1 for k in uniq_elem: max_repeat = max(max_repeat, new_lst.count(k)) # 用最小重复次数求解,如果不行,元素重复次数加1,再求解 min_lst = [] for i in range(1, max_repeat+1): for k in uniq_elem: if k in new_lst: new_lst.remove(k) min_lst.append(k) if sum(min_lst) == n: return min_lst print('round:', i, 'len(lst):', len(min_lst)) # 减少排列组合次数 for i in sorted(uniq_elem, reverse=True): n0 = min_lst[0] n1 = n - n0 ans = comb(n1, min_lst[1:]) if ans: return [n0] + list(ans) a.sort() # b.sort(reverse=True) assert sum(a) == sum(b) for i in a: ans = resolve(i, b) if ans: print(f'{i} = {" + ".join(map(str, ans))}') [ b.remove(i) for i in ans ]