老王装货 最优化问题

#! /usr/bin/env python
#coding:utf8
import itertools
list1=[509,838,924,650,604,793,564,651,697,649,747,787,701,605,644]
#对数据正序排序
list1.sort()
#该函数用于计算最多或者最少容纳的最优个数
def return_num(num_list):
    num,count=0,0
    for i in list1:
        count=count+i
        if count<=5000:
            num=num+1
        else:
            continue
    return  num
#给出特定数的最优无序组合
def return_data(input_list,num,current_sum=0):
    zuhe_all=list(itertools.combinations(input_list,num))
    for i in zuhe_all:
        count=sum(i)
        if count>current_sum and count<=5000:
            current_sum=count
            print i,current_sum

#计算出满足条件时最多能容纳的个数
max_num=return_num(list1)
print max_num
#将正序逆序
list1.reverse()
#计算出满足条件时最少需要多少才更接近理想值
min_num=return_num(list1)
print min_num
current_sum=0
for num in range(min_num,max_num+1):
    current_sum=return_data(list1,num,current_sum)

你可能感兴趣的:(老王装货 最优化问题)