我需要编写一个程序来计算最便宜的大巴和小巴组合来运送一定数量的乘客,这是用户给定的。一辆载有 48 名乘客的大巴士租金为 200 美元,一辆载有 10 名乘客的小型巴士租金为 95 美元。我有一个解决方案可以计算许多大小的公共(public)汽车,但我不知道如何考虑事物的成本方面。这是我到目前为止的代码:
passengers = int(input("How many passengers? "))
bigBus = 0
smallBus = 0
while passengers > 0:
if passengers / 48 > 1:
passengers -= 48
bigBus += 1
else:
passengers -= 10
smallBus += 1
cost = (bigBus * 200) + (smallBus * 95)
print("Hire", bigBus, "big buses and", smallBus, "small buses.")
print("Cost =", cost)
在 if 语句中将乘客除以 48 是问题的原因,因为您可以在 1 辆大巴士(例如 30 辆)中运送少于 48 名乘客,并且比在 3 辆小型公共(public)汽车上运送乘客便宜,但我不知道在那种情况下我将如何确定更便宜的成本。我认为解决方案就在我的脑海中。
请您参考如下方法:
每位乘客的平均费用是 200/48和 95/10如果公共(public)汽车满了。显然,更大的巴士更经济,所以最好的解决方案是使用更多的大型巴士。
n_large = 0
n_small = 0
n_large += passengers // 48
# people remain unsettled
# [0, 48]
passengers = passengers % 48
但您还需要计算末类车的费用,因为它没有满员:if 200/num_people_left > 95/num_people_left然后为最后几个人使用较小的公共(public)汽车。
