📌  相关文章
📜  查找是否有可能从给定的成本和数量范围获得比率

📅  最后修改于: 2021-04-24 05:06:33             🧑  作者: Mango

考虑到成本的低成本来自于upCost的范围,范围从lowQuantupQuant量,发现如果有可能获得一个给定的配给比例r其中r = \frac{cost}{quantity}  ,并且lowCost <=成本<= upCost和lowQuant <=数量<= upQuant。
例子 :

Input : lowCost = 1, upCost = 10, 
        lowQuant = 2, upQuant = 8
        r = 3
Output : Yes
Explanation:
cost / quantity = 6 / 2 = 3
where cost is in [1, 10] and quantity
is in [2, 8]

Input : lowCost = 14, upCost = 30, 
        lowQuant = 5, upQuant = 12
        r = 9
Output : No

方法:从给定的公式,可以轻松推导出以下公式: cost = quantity*r
从这个方程式,可以很容易地推导出逻辑。用r检查每个数量值的乘积,如果乘积的任何值在lowCost和upCost之间,则答案为“是”,否则为“否”。
下面是上述方法的实现:

C++
// C++ program to find if it is
// possible to get the ratio r
#include 
using namespace std;
 
// Returns true if it is
// possible to get ratio r
// from given cost and
// quantity ranges.
bool isRatioPossible(int lowCost, int upCost,
                     int lowQuant, int upQuant,
                     int r)
{
    for (int i = lowQuant; i <= upQuant; i++)
    {
 
        // Calculating cost corresponding
        // to value of i
        int ans = i * r;
        if (lowCost <= ans && ans <= upCost)
            return true;
    }
 
    return false;
}
 
// Driver Code
int main()
{
    int lowCost = 14, upCost = 30,
        lowQuant = 5, upQuant = 12,
        r = 9;
 
    if (isRatioPossible(lowCost, upCost,
                        lowQuant, upQuant, r))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// Java program to find if it is
// possible to get the ratio r
import java.io.*;
 
class Ratio
{
 
    // Returns true if it is
    // possible to get ratio r
    // from given cost and
    // quantity ranges.
    static boolean isRatioPossible(int lowCost, int upCost,
                                   int lowQuant, int upQuant,
                                   int r)
    {
        for (int i = lowQuant; i <= upQuant; i++)
        {
 
            // Calculating cost corresponding
            // to value of i
            int ans = i * r;
            if (lowCost <= ans && ans <= upCost)
                return true;
        }
 
        return false;
    }
 
    // Driver Code
    public static void main(String args[])
    {
        int lowCost = 14, upCost = 30,
            lowQuant = 5, upQuant = 12, r = 9;
 
        if (isRatioPossible(lowCost, upCost,
                            lowQuant, upQuant, r))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}


Python3
# Python 3 program to find if it
# is possible to get the ratio r
 
# Returns true if it is
# possible to get ratio r
# from given cost and
# quantity ranges.
def isRatioPossible(lowCost, upCost,
                    lowQuant, upQuant, r) :
     
    for i in range(lowQuant, upQuant + 1) :
         
        # Calculating cost corresponding
        # to value of i
        ans = i * r
         
        if (lowCost <= ans and ans <= upCost) :
            return True
             
    return False
 
     
# Driver Code
lowCost = 14; upCost = 30
lowQuant = 5; upQuant = 12; r = 9
 
if (isRatioPossible(lowCost, upCost,
                    lowQuant,upQuant, r)) :
    print( "Yes" )
else :
    print( "No" )
     
# This code is contributed
# by Nikita Tiwari.


C#
// C# program to find if it is
// possible to get the ratio r
using System;
 
class Ratio
{
 
    // Returns true if it is
    // possible to get ratio r
    // from given cost and
    // quantity ranges.
    static bool isRatioPossible(int lowCost, int upCost,
                                int lowQuant, int upQuant,
                                int r)
    {
        for (int i = lowQuant; i <= upQuant; i++)
        {
 
            // Calculating cost corresponding
            // to value of i
            int ans = i * r;
            if (lowCost <= ans && ans <= upCost)
                return true;
        }
 
        return false;
    }
 
    // Driver Code
    public static void Main()
    {
        int lowCost = 14, upCost = 30,
            lowQuant = 5, upQuant = 12, r = 9;
 
        if (isRatioPossible(lowCost, upCost,
                            lowQuant, upQuant, r))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出 :

No