📌  相关文章
📜  要加到X的最小值,使得它至少是N的Y百分比

📅  最后修改于: 2021-04-24 15:52:11             🧑  作者: Mango

给定三个整数NXY ,任务是找到应加到X的最小整数,以使其至少占N的Y百分比。

例子:

方法:找到val =(N * Y)/ 100 ,它是NY百分比。现在,为了使X等于val ,仅当X 必须将val – X添加到X上

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the required value
// that must be added to x so that
// it is at least y percent of n
int minValue(int n, int x, int y)
{
  
    // Required value
    float val = (y * n) / 100;
  
    // If x is already >= y percent of n
    if (x >= val)
        return 0;
    else
        return (ceil(val) - x);
}
  
// Driver code
int main()
{
    int n = 10, x = 2, y = 40;
    cout << minValue(n, x, y);
}


Java
// Java implementation of the approach
import java.lang.Math;
  
class GFG 
{
      
// Function to return the required value
// that must be added to x so that
// it is at least y percent of n
static int minValue(int n, int x, int y)
{
  
    // Required value
    float val = (y * n) / 100;
  
    // If x is already >= y percent of n
    if (x >= val)
        return 0;
    else
        return (int)(Math.ceil(val)-x);
}
  
// Driver code
public static void main(String[] args)
{
    int n = 10, x = 2, y = 40;
    System.out.println(minValue(n, x, y));
}
}
  
// This code is contributed by Code_Mech.


Python3
import math
  
# Function to return the required value 
# that must be added to x so that 
# it is at least y percent of n 
def minValue(n, x, y):
  
    # Required value
    val = (y * n)/100
  
    # If x is already >= y percent of n 
    if x >= val:
        return 0
    else:
        return math.ceil(val) - x
  
# Driver code
n = 10; x = 2; y = 40
print(minValue(n, x, y))
  
  
# This code is contributed by Shrikant13


C#
// C# implementation of the approach 
using System;
  
class GFG 
{ 
      
    // Function to return the required value 
    // that must be added to x so that 
    // it is at least y percent of n 
    static int minValue(int n, int x, int y) 
    { 
      
        // Required value 
        float val = (y * n) / 100; 
      
        // If x is already >= y percent of n 
        if (x >= val) 
            return 0; 
        else
            return (int)(Math.Ceiling(val)-x) ; 
    } 
      
    // Driver code 
    public static void Main() 
    { 
        int n = 10, x = 2, y = 40; 
        Console.WriteLine((int)minValue(n, x, y)); 
    } 
} 
  
// This code is contributed by Ryuga.


PHP
= y percent of n
    if ($x >= $val)
        return 0;
    else
        return (ceil($val) - $x);
}
  
// Driver code
{
    $n = 10; $x = 2; $y = 40;
    echo(minValue($n, $x, $y));
}
  
// This code is contributed by Code_Mech.


输出:
2

时间复杂度: O(1)