运行不了
时间: 2024-02-29 07:53:04 浏览: 102
非常抱歉,可能是因为我在回答中使用了 Markdown 语法,导致代码格式出现了问题。请使用以下代码进行尝试:
```python
profits = [
[0, 3, 5, 6, 7, 6, 5],
[0, 4, 6, 7, 8, 9, 10],
[0, 2, 5, 9, 8, 8, 7]
]
num_devices = 5
num_factories = 3
# 先分配每个工厂一套设备
devices_per_factory = [1] * num_factories
devices_left = num_devices - num_factories
# 按照每个工厂的利润从大到小排序
sorted_profits = sorted(enumerate(profits), key=lambda x: max(x[1]), reverse=True)
# 依次将剩余设备分配给利润最高的工厂
for i in range(devices_left):
factory = sorted_profits[i % num_factories][0]
devices_per_factory[factory] += 1
# 计算总利润
total_profit = 0
for i in range(num_factories):
total_profit += profits[i][devices_per_factory[i]]
print("订购{}套设备,分配方案为{},每年预计创利总额为{}万元。".format(num_devices, devices_per_factory, total_profit))
```
如果还有问题,请告诉我,我会及时回答。
阅读全文