Question:
有两个序列a,b,大小都为n,序列元素的值任意整形数,无序;
要求:通过交换a,b中的元素,使[序列a元素的和]与[序列b元素的和]之间的差最小。(注:整形数 是指 整数)
Answer:
def look(a, max = 0):
a.sort()
while 1:
if len(a) == 0:return None
val = a.pop()
if val <= max:return val
def ab(a , b):
a.extend(b)
sum = reduce(lambda x,y:x+y, a)
arvg = sum / 2.0
rs1 = []
def qq(arvg, pick = None):
if pick is None:
pick = a.pop()
else:
a.remove(pick)
rs1.append(pick)
arvg -= pick
pick = look(a[:], max = arvg)
if pick is not None:qq(arvg, pick)
qq(arvg)
print rs1, a
ab([1,2,5,3],[6,7,8,9,10])