You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

实现出租车计价对比函数:判断最优出行选择

出租车公司计价对比函数实现方案

问题描述

需求:实现一个函数,接收两家出租车公司的固定费用(fixed_X、fixed_Y)与每公里费用(fare_per_km_X、fare_per_km_Y),根据计价公式:
company X:final_value_X = fixed_X + (km_traveled*fare_per_km_X)
company Y:final_value_Y = fixed_Y + (km_traveled*fare_per_km_Y)
返回指定格式的字符串,告知用户最优选择:

  1. "Whatever":任意里程下两家费用相等
  2. "company X":任意里程下X公司费用更低
  3. "company Y":任意里程下Y公司费用更低
  4. "Company X when the distance < N, Whatever when the distance = N, Company Y when the distance > N":最优选择取决于行驶里程
    当前问题:未给定行驶里程时,如何计算第4种情况?现有代码仅对比有限里程,不知如何完善。现有代码如下:
def company_fare_compare(fixed_X,fare_per_km_X,fixed_Y,fare_per_km_Y):
    km = 0.0
    cal_valor_taxi1 = float(tf1) + (km*float(vqr1))
    cal_valor_taxi2 = float(tf2) + (km*float(vqr2))
    while km <= 10:
        cal_valor_taxi1
        cal_valor_taxi2
        if cal_valor_taxi1 < cal_valor_taxi2:
            print("Empresa 1")
        if cal_valor_taxi1 == cal_valor_taxi2:
            print("Whatever")
        if cal_valor_taxi1 > cal_valor_taxi2:
            print("Empresa 2")
        km += 1

解决方案

核心思路

不用遍历有限里程,直接通过代数解方程找到两家费用相等的里程平衡点N,再结合两家的每公里费用斜率,判断不同里程区间的最优选择。

分情况逻辑

  1. 任意里程费用相等:当两家的固定费用和每公里费用都完全相等时,即fixed_X == fixed_Y且fare_per_km_X == fare_per_km_Y,无论跑多少公里费用都一致。
  2. X始终更便宜:满足以下任一条件即可:
    • X的每公里费用更低,且初始固定费用不高于Y:fare_per_km_X < fare_per_km_Y且fixed_X <= fixed_Y
    • 每公里费用相同,但X的固定费用更低:fare_per_km_X == fare_per_km_Y且fixed_X < fixed_Y
  3. Y始终更便宜:逻辑与X始终更便宜相反:
    • Y的每公里费用更低,且初始固定费用不高于X:fare_per_km_Y < fare_per_km_X且fixed_Y <= fixed_X
    • 每公里费用相同,但Y的固定费用更低:fare_per_km_Y == fare_per_km_X且fixed_Y < fixed_X
  4. 费用随里程变化:当两家的每公里费用不同,且固定费用的优势会被每公里费用抵消时,解方程fixed_X + N*fare_per_km_X = fixed_Y + N*fare_per_km_Y,得到平衡点:
    N = (fixed_Y - fixed_X) / (fare_per_km_X - fare_per_km_Y)
    
    再根据每公里费用判断区间:
    • 若fare_per_km_X < fare_per_km_Y:X每公里成本更低,短里程选X;当里程超过N后,Y的低固定费用优势会覆盖每公里成本劣势,选Y
    • 若fare_per_km_X > fare_per_km_Y:Y每公里成本更低,短里程选Y;里程超过N后,X的低固定费用优势覆盖成本劣势,选X

完整实现代码

def company_fare_compare(fixed_X, fare_per_km_X, fixed_Y, fare_per_km_Y):
    # 统一转换为浮点数,避免类型错误
    fx = float(fixed_X)
    fpx = float(fare_per_km_X)
    fy = float(fixed_Y)
    fpy = float(fare_per_km_Y)
    
    # 情况1:任意里程费用相等
    if fx == fy and fpx == fpy:
        return "Whatever"
    
    # 情况2:X始终更便宜
    if (fpx < fpy and fx <= fy) or (fpx == fpy and fx < fy):
        return "company X"
    
    # 情况3:Y始终更便宜
    if (fpy < fpx and fy <= fx) or (fpy == fpx and fy < fx):
        return "company Y"
    
    # 情况4:计算费用平衡点N
    numerator = fy - fx
    denominator = fpx - fpy
    n = numerator / denominator
    
    # 根据每公里费用判断区间最优选择
    if fpx < fpy:
        return f"Company X when the distance < {n}, Whatever when the distance = {n}, Company Y when the distance > {n}"
    else:
        return f"Company Y when the distance < {n}, Whatever when the distance = {n}, Company X when the distance > {n}"

代码说明

  • 先处理三种简单情况,减少不必要的计算
  • 平衡点通过代数推导直接求解,无需遍历里程,效率更高
  • 返回字符串严格符合需求格式,若需要格式化N的小数位数,可改用round(n, 2)等方式调整

内容的提问来源于stack exchange,提问作者Luna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 13:41:42