将集合划分为两个子集,以使子集和的差为:
minimum(split numbers from A list to B and C so that sum(B) - sum(C) will be minimum among all other options)
位置:
A = [5,6,7,8], B = [5,8], C = [6,7]
我想您正在寻找这个?
def partition(S):
A = []
B = []
for i in sorted(S, reverse=True):
if sum(A) < sum(B):
A.append(i)
else:
B.append(i)
return (A, B)
S=[5,6,7,8]
print(partition(S))