Python 货物分配问题

我有一堆货物,重量分别为 : 6,6,5,5,5,4,4,4,4,2,2,2,2,3,3,7,7,5,5,8,8,4,4,5 我需要把这些货物,装入最大重量为 9 的箱子里面(重量之和小于等于 9 的货物可以装到一 个箱子里面)。请问,我最少需要多少箱子,才能把这些货物完全装入进去?

thing = [6, 6, 5, 5, 5, 4, 4, 4, 4, 2, 2, 2, 2, 3, 3, 7, 7, 5, 5, 8, 8, 4, 4, 5]
target = 9


def match(things, sum):
    global x   # x为装箱数
    x = 0
    for i in range(len(things)):
        for j in range(i + 1, len(things)):
            if things[i] + things[j] == sum:
                print([things[i], things[j]])
                things[i] = 9
                things[j] = 9
                x = x + 1

    for i in range(len(things)):
        for j in range(i + 1, len(things)):
            if things[i] + things[j] < sum:
                print((things[i], things[j]))
                things[i] = 9
                things[j] = 9
                x = x + 1

    for i in range(len(things)):
        if things[i] < sum:
            print({things[i]})
            x = x + 1
    print("共要装%d箱" % x)

if __name__ == '__main__':
    match(thing, target)

当货物被统计一次后就被赋值为9,之后就不会被重复统计。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值