Chinaunix首页 | 论坛 | 博客
  • 博客访问: 91588
  • 博文数量: 30
  • 博客积分: 1235
  • 博客等级: 中尉
  • 技术积分: 295
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-20 21:15
文章分类

全部博文(30)

文章存档

2010年(8)

2009年(22)

我的朋友

分类: Python/Ruby

2009-11-06 14:21:34


#!/usr/bin/env python

#求两序列的和最小差值序列


def meanList(sorted_list):
    global big_list
    global small_list

    if not sorted_list:
        return [],[]

    #print("small = {0}\tbig={1}".format(sorted_list[-2],sorted_list[-1]))

    meanList(sorted_list[:-2])

    if(sum(big_list) < sum(small_list)):
        big_list.append(sorted_list[-1])
        small_list.append(sorted_list[-2])
    else:
        big_list.append(sorted_list[-2])
        small_list.append(sorted_list[-1])

    #if sum(big_list) < sum(small_list):
    # tmp = small_list
    # small_list = big_list
    # big_list = tmp



#sorted_list = [1,2,3,4,5,6,7,8,9,10]

test_list = [12312, 12311, 232, 555, 3550, 29, 3, 2, 1, 566,6434,34]
if len(test_list) % 2:
    test_list.append(0)

test_list.sort()
#sorted_list.reverse()
#print(test_list)

big_list = []
small_list = []
meanList(test_list)

print(big_list)
print("sum of big list is\t",sum(big_list))

print(small_list)
print("sum of small list is\t",sum(small_list))

print(str(sum(big_list)) + "-" + str(sum(small_list)) + "=" + str(sum(big_list) - sum(small_list) ) )



阅读(645) | 评论(0) | 转发(0) |
0

上一篇:python习题1

下一篇:python习题:排序算法

给主人留下些什么吧!~~