高分求python大神帮忙解答,实在有难度,做不出来

2024-12-31 04:51:43
推荐回答(2个)
回答1:

"""用itertools的组合工具combinations生成所有组合方案"""

import itertools

def sameSums(alst):
    allsummary = sum(alst)
    if allsummary % 2:
        # 数组总和不能被2整除既不可均分为等值的两部分
        return False
    halfsum = allsummary / 2  # 总和的半值为检测目标
    for l in xrange(1, len(alst)/2+1):
        # 方案第一部分的数组长度从1位到数组半长
        for sub in list(itertools.combinations(alst, l)):
            # 按指定长度遍历组合方案
            if sum(sub) == halfsum:
                # 若当前方案的和为目标值则当前方案有效
                # another为方案的第二部分数据列表
                another = alst[:]
                for x in sub:
                    another.pop(another.index(x))
                print sub, tuple(another)                
                return True
    return False


def optsamesum(alst):
    """优化一下"""
    allsummary = sum(alst)
    if allsummary % 2:
        return False
    halfsum = allsummary / 2
    if max(alst) > halfsum:
        return False
    sortedlst = sorted(alst)[::-1]
    for l in xrange(1, len(alst)/2+1):
        for sub in list(itertools.combinations(sortedlst, l)):
            if sum(sub) < halfsum:
                break
            if sum(sub) == halfsum:
                another = sortedlst[:]
                for x in sub:
                    another.pop(another.index(x))
                print list(sub), another
                return True
    return False

if __name__ == "__main__":
    sameSums([4, 7, 6, 3])
    sameSums([3, 3])
    sameSums([4, 12, 16])
    sameSums([5, 1])

回答2:

当做一个背包问题来解,判断最大装入是否等于背包的容量,算法具体意义请百科,示例如下:
def test(a):
total_value = sum(a)
if sum(a)%2 != 0:
return False
#背包载重量
m = total_value/2
#n的个数
n=len(a)-1
x = [False for raw in range(n + 1)]
v = 0
optp = [[0 for col in range(m + 1)] for raw in range(n + 1)]
#计算optp[i][j]
for i in range(1, n + 1):
for j in range(1, m + 1):
optp[i][j] = optp[i - 1][j]
if (j >= a[i]) and (optp[i - 1][j - a[i]] + a[i] > optp[i - 1][j]):
optp[i][j] = optp[i - 1][j - a[i]] + a[i]
#递推装入背包的物体
j = m
for i in range(n, 0, -1):
if optp[i][j] > optp[i - 1][j]:
x[i] = True
j = j - a[i]
#返回最大价值
v = optp[n][m]
return v == m
#test...
a = [21,5,38,11,10,17,15,27,25,44,22,8,26,13,16,\
37,1,24,31,19,2,14,28,3,33,23,43,20,12,14]
b = [4,7,6,3]
c = [3,3]
d = [5,1]
e = [4,12,16]
print test(a)
print test(b)
print test(c)
print test(d)
print test(e)
#...test