回复 104楼 rolimi
我想我是用平均值而不是使用包容量的贪心值达到目前状态的。至少我现在还没有找到击破该算法获取最优值的数据组合,希望各位帮忙测试下(我觉得关键点在我数据回溯只有一层,当组合超过3个以上时,我觉得得不到最优组合,问题是该是什么样的源数据才能测试的到呢)。

能编个毛线衣吗?

2015-06-16 17:03

2015-06-16 18:36
2015-06-17 07:36
2015-06-17 07:42
程序代码:
# -*- coding:utf-8 -*-
import random
def spawn_random_group(count, capacity):
total = count * capacity - random.randint(0, capacity-1)
group = []
while count > 0:
weight = total - (count-1)*capacity
total -= weight
while weight > 0:
n = random.randint(1, weight)
group.append(str(n))
weight -= n
count -= 1
return group
def main():
count = 10 #int(raw_input("输入宝箱数量:"))
capacity = 20 #int(raw_input("输入包裹容量:"))
groups_count = 10 #int(raw_input("输入需要产生的数据组数:"))
for i in xrange(groups_count):
group = spawn_random_group(count, capacity)
print capacity
print len(group)
print ' '.join(group)
if __name__ == '__main__':
main()

2015-06-17 11:30

2015-06-17 11:40

2015-06-17 12:05
2015-06-17 13:12
2015-06-19 09:41
2015-06-19 13:45