找出是否有可能从给定的成本和数量范围中得到一个比率

考虑到从 低成本 上涨成本 数量范围从 低质量 上流社会 ,找出是否有可能获得给定的配给率 R 哪里 r = frac{cost}{quantity}   ,而且成本低<= 费用 <=成本较高且数量较低<= <=upQuant。 例如:

null
Input : lowCost = 1, upCost = 10,         lowQuant = 2, upQuant = 8        r = 3Output : YesExplanation:cost / quantity = 6 / 2 = 3where cost is in [1, 10] and quantityis in [2, 8]Input : lowCost = 14, upCost = 30,         lowQuant = 5, upQuant = 12        r = 9Output : No

方法: 根据给定的公式,可以很容易地推导出以下方程式: cost = quantity*r   . 从这个等式可以很容易地推断出逻辑。用r检查每个数量值的产品,如果产品的任何值介于低成本和高成本之间,则回答是“是”,否则回答是“否”。 以下是上述方法的实施情况:

C++

// C++ program to find if it is
// possible to get the ratio r
#include <bits/stdc++.h>
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

<?php
//PHP 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.
function isRatioPossible( $lowCost , $upCost ,
$lowQuant , $upQuant , $r )
{
for ( $i = $lowQuant ; $i <= $upQuant ; $i ++)
{
// Calculating cost corresponding
// to value of i
$ans = $i * $r ;
if ( $lowCost <= $ans && $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 ))
echo "Yes" ;
else
echo "No" ;
# This code is contributed by ajit
?>


Javascript

<script>
// JavaScript 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.
function isRatioPossible(lowCost, upCost,
lowQuant, upQuant,
r)
{
for (let i = lowQuant; i <= upQuant; i++)
{
// Calculating cost corresponding
// to value of i
let ans = i * r;
if (lowCost <= ans && ans <= upCost)
return true ;
}
return false ;
}
// Driver code
let lowCost = 14, upCost = 30,
lowQuant = 5, upQuant = 12, r = 9;
if (isRatioPossible(lowCost, upCost,
lowQuant, upQuant, r))
document.write( "Yes" );
else
document.write( "No" );
</script>


输出:

No

时间复杂性: O(|uq lq |),其中uq为上量,lq为低量。

辅助空间: O(1)

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享