用python计算,有一个case(订单),有7个l(长)为170, w(宽)为110, h(高)为27, 和1个l(长)为 210,w(宽)为 200,h(高)为 30的 物品,选择装载在 普通1号自营纸箱 箱 165 120 55 普通2号自营纸箱 箱 200 140 70 普通3号自营纸箱 箱 200 150 150 普通4号自营纸箱 箱 270 200 90 普通5号自营纸箱 箱 300 200 170 中,相同的case(订单)必须放在一个箱子中,要求使用耗材数量越少越好, 在耗材数量相同时,耗材总体积越小越好。输出每种耗材的使用总数和耗材总体 积。
时间: 2023-11-13 20:05:02 浏览: 86
首先,我们可以将所有箱子的尺寸和耗材数量存储在一个字典中:
```
boxes = {
"普通1号自营纸箱": {"l": 165, "w": 120, "h": 55, "count": 0, "volume": 0},
"普通2号自营纸箱": {"l": 200, "w": 140, "h": 70, "count": 0, "volume": 0},
"普通3号自营纸箱": {"l": 200, "w": 150, "h": 150, "count": 0, "volume": 0},
"普通4号自营纸箱": {"l": 270, "w": 200, "h": 90, "count": 0, "volume": 0},
"普通5号自营纸箱": {"l": 300, "w": 200, "h": 170, "count": 0, "volume": 0}
}
```
然后,我们可以用一个函数来计算每个物品所需的箱子数量,并将其放入相应的箱子中:
```
def pack_item(item, boxes):
for box in boxes:
if item["l"] <= boxes[box]["l"] and item["w"] <= boxes[box]["w"] and item["h"] <= boxes[box]["h"]:
boxes[box]["count"] += 1
boxes[box]["volume"] += boxes[box]["l"] * boxes[box]["w"] * boxes[box]["h"]
return boxes
return None
```
接下来,我们可以循环遍历所有物品,并将它们放入箱子中:
```
items = [
{"l": 170, "w": 110, "h": 27},
{"l": 170, "w": 110, "h": 27},
{"l": 170, "w": 110, "h": 27},
{"l": 170, "w": 110, "h": 27},
{"l": 170, "w": 110, "h": 27},
{"l": 170, "w": 110, "h": 27},
{"l": 210, "w": 200, "h": 30}
]
for item in items:
boxes = pack_item(item, boxes)
```
最后,我们可以循环遍历所有箱子,输出它们的耗材数量和总体积:
```
for box in boxes:
print(box)
print("耗材数量:", boxes[box]["count"])
print("耗材总体积:", boxes[box]["volume"])
print("--------------------")
```
完整代码如下:
```
boxes = {
"普通1号自营纸箱": {"l": 165, "w": 120, "h": 55, "count": 0, "volume": 0},
"普通2号自营纸箱": {"l": 200, "w": 140, "h": 70, "count": 0, "volume": 0},
"普通3号自营纸箱": {"l": 200, "w": 150, "h": 150, "count": 0, "volume": 0},
"普通4号自营纸箱": {"l": 270, "w": 200, "h": 90, "count": 0, "volume": 0},
"普通5号自营纸箱": {"l": 300, "w": 200, "h": 170, "count": 0, "volume": 0}
}
def pack_item(item, boxes):
for box in boxes:
if item["l"] <= boxes[box]["l"] and item["w"] <= boxes[box]["w"] and item["h"] <= boxes[box]["h"]:
boxes[box]["count"] += 1
boxes[box]["volume"] += boxes[box]["l"] * boxes[box]["w"] * boxes[box]["h"]
return boxes
return None
items = [
{"l": 170, "w": 110, "h": 27},
{"l": 170, "w": 110, "h": 27},
{"l": 170, "w": 110, "h": 27},
{"l": 170, "w": 110, "h": 27},
{"l": 170, "w": 110, "h": 27},
{"l": 170, "w": 110, "h": 27},
{"l": 210, "w": 200, "h": 30}
]
for item in items:
boxes = pack_item(item, boxes)
for box in boxes:
print(box)
print("耗材数量:", boxes[box]["count"])
print("耗材总体积:", boxes[box]["volume"])
print("--------------------")
```
阅读全文