题目为:
有两个序列a,b,大小都为n,序列元素的值任意整形数,无序;
要求:通过交换a,b中的元素,使[序列a元素的和]与[序列b元素的和]之间的差最小
- #!/usr/bin/python
- #coding=utf-8
- ######Author: Yao######
- a = [1,10,20,22,23]
- b = [5,3,13,4,111]
- c = a + b
- c.sort()
- print c
- x = []
- y = []
- x.append(c.pop())
- y.append(c.pop())
- while len(x) <len(a) and len(y) < len(b):
- if sum(x) > sum(y) and c:
- y.append(c.pop())
- else:
- x.append(c.pop())
- if len(x)==len(a):
- print x,y+c
- else:
- print x+c,y