如何解决以下问题,是否需要穷举枚举或递归?我在下面附上了失败的代码。
n人在地铁上。众所周知,每个人都需要进行m次旅行。地铁票分为三种:
一张单程票,花费一个卢布。
一个人的无限票,费用为b卢布。
k人的团体无限票,费用为c卢布。
输入数据格式:
第一行有 3 个整数n、m和k ( 1 ≤ n , m , k ≤ 1000)
第二行有 3 个整数a、b和c (1 ≤ a , b , c ≤ 1000)
输出格式:打印单个整数 - 门票的最低总成本。
例子:
входные данные
7 4 5
2 7 20
выходные данные
34
失败的代码:
n,m,k = 7,4,5
a,b,c = 2,7,20
cost_1 = a
cost_2 = b/m
cost_3 = (c/m)/k
costs = {"cost_1": cost_1, "cost_2": cost_2, "cost_3": cost_3}
min_cost = None
ticket = ''
for key in costs:
if min_cost is None or costs[key] < min_cost:
min_cost = costs[key]
ticket = key
result = 0
if ticket == "cost_1":
result = cost_1 * m * n
elif ticket == "cost_2":
result = b * n
elif ticket == "cost_3":
if n % k == 0:
result = n/k * c
else:
temp = n//k
temp_2 = n - (temp * k)
min_second_cost = None
second_ticket = ''
for key in costs:
if key == ticket:
continue
else:
min_second_cost = costs[key]
second_ticket = key
temp_result = 0
if second_ticket == "cost_1":
temp_result = cost_1 * m * temp_2
elif second_ticket == "cost_2":
temp_result = b * temp_2
result = temp_result + temp * k
print(result)
1 个回答