查找是否有可能在Python中从给定的成本和数量范围获得比率
假设我们的成本范围从lowCost到upCost,数量范围从lowQuant到upQuant,我们必须检查是否可以找到给定比率r,其中r=成本/数量,以及lowCost成本=upCost和lowQuant数量⇐upQuant。
因此,如果输入像lowCost=2,upCost=10,lowQuant=3,upQuant=9且r=3,则输出将为True,因为成本=r*数量=3*3=9,其中成本在范围[1,10]和数量在[2,8]
为了解决这个问题,我们将遵循以下步骤-
对于范围在l_quant到u_quant的我,执行
返回True
res:=i*比率
如果l_cost⇐res和res⇐u_cost,则
返回False
示例
让我们看下面的实现以更好地理解-
def can_we_find_r(l_cost, u_cost, l_quant, u_quant, ratio) :
for i in range(l_quant, u_quant + 1) :
res = i * ratio
if (l_cost <= res and res <= u_cost) :
return True
return False
l_cost = 2
u_cost = 10
l_quant = 3
u_quant = 9
ratio = 3
print(can_we_find_r(l_cost, u_cost,l_quant,u_quant, ratio))输入值
2, 10, 3, 9, 3
输出结果
True
热门推荐
10 最美的留言祝福语简短
11 六一加端午祝福语简短
12 创业大吉祝福语简短
13 姨父生日祝福语简短精辟
14 周末愉快祝福语高级简短
15 女性生日祝福语简短优美
16 宝宝新年祝福语大全简短
17 年后祝福语简短走心
18 妈妈生日贺卡祝福语 简短