[英]Python: How do you find the simplest form of divisibility
I am working on a program where the user inputs the number of people going on a trip and the program determines the required number of each vehicle type to be used.我正在开发一个程序,其中用户输入旅行的人数,该程序确定要使用的每种车辆类型所需的数量。 Saying that: Car - 4 passengers Minivan - 7 passengers Small Bus - 15 passengers Big Bus - 60 passengers
说:汽车 - 4 人小型货车 - 7 人小型巴士 - 15 人大型巴士 - 60 人
This is what I have so far:这是我到目前为止:
car_capacity = int(4)
minivan_capacity = int(7)
short_bus_capacity = int(15)
full_size_bus_capacity = int(60)
cars_needed = 0
minivan_needed = 0
short_buses_needed = 0
full_size_buses_needed = 0
passengers = int(input("How many people will be going?\n"))
if passengers < 1:
print("Nobody is going, therefore no vehicles will be needed.")
while passengers > 0:
print(passengers % 60)
Basically I'm trying to find out the fewest Vehicles needed.基本上,我试图找出所需的最少车辆。 Ex (Input = 63. Output 1 Big Bus, 1 Small)
Ex(输入 = 63。输出 1 大总线,1 小总线)
Please don't think I'm asking for answers, I just need a clue or a hint of what to research or how to approach this请不要以为我是在寻求答案,我只需要一个线索或暗示要研究什么或如何解决这个问题
passengers = int(input("How many people will be going?\n"))
if passengers < 1:
print("Nobody is going, therefore no vehicles will be needed.")
# make it a dictionary, so we can loop through
vehicles = {
4: 'car',
7: 'minivan',
15: 'short_bus',
60: 'full_size_bus'
}
vehicles_needed = {}
# a helper to check one type of vehicle
def check_vehicle(passengers, capacity, name):
if passengers >= capacity:
amount = passengers / capacity
# substract the passengers
passengers -= (amount * capacity)
return {name: amount}, passengers
return {}, passengers
# loop through all capacities from the biggest one
for k, v in sorted(vehicles.items(), reverse=True):
# if any vehicle needed, it will be put in the vehicles_needed
vehicle_update, passengers = check_vehicle(passengers, k, v)
vehicles_needed.update(vehicle_update)
# if there is a leftover, add one car
if passengers > 0:
vehicles_needed.update({'car': vehicles_needed.get('car', 0) + 1})
print vehicles_needed # 67 -> {'minivan': 1, 'full_size_bus': 1}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.