Cho khoảng chi phí [lowCost, upCost] và khoảng số lượng [lowQuant, upQuant], kiểm tra có tồn tại cost, quantity trong khoảng sao cho ratio = cost/quantity bằng r cho trước.
Ví dụ: lowCost=2, upCost=10, lowQuant=3, upQuant=9, r=3 → True (quantity=3, cost=9 ∈ [2,10]).
Thuật toán
- Duyệt quantity từ lowQuant đến upQuant.
- Tính
res = quantity * ratio. - Nếu
lowCost ≤ res ≤ upCost→ trả về True. - Hết vòng lặp → False.
Ví dụ code
def can_we_find_r(l_cost, u_cost, l_quant, u_quant, ratio):
for q in range(l_quant, u_quant + 1):
res = q * ratio
if l_cost <= 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))
Input
2, 10, 3, 9, 3
Output
True