📜  求出将给定比例a:b加到a:b后比例变为c:d的数字

📅  最后修改于: 2021-05-31 16:37:36             🧑  作者: Mango

给定四个整数abcd 。任务是找到数字X ,将其与数字ab相加后,其比率将从a:b变为c:d
例子:

方法:旧比例为a:b ,新比例为c:d 。令所需的数字为X
因此, (a + X)/(b + X)= c / d
或者,ad + dx = bc + cx
或者,x(d – c)= bc –广告
因此, x =(bc – ad)/(d – c)
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the
// required number X
int getX(int a, int b, int c, int d)
{
    int X = (b * c - a * d) / (d - c);
    return X;
}
 
// Driver code
int main()
{
    int a = 2, b = 3, c = 4, d = 5;
 
    cout << getX(a, b, c, d);
 
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
 
class GFG
{
     
// Function to return the
// required number X
static int getX(int a, int b, int c, int d)
{
    int X = (b * c - a * d) / (d - c);
    return X;
}
 
// Driver code
public static void main (String[] args)
{
 
    int a = 2, b = 3, c = 4, d = 5;
    System.out.println (getX(a, b, c, d));
 
}
}
 
// The code is contributed by ajit..@23


Python
# Python3 implementation of the approach
 
# Function to return the
# required number X
def getX(a, b, c, d):
 
    X = (b * c - a * d) // (d - c)
    return X
 
# Driver code
 
a = 2
b = 3
c = 4
d = 5
 
print(getX(a, b, c, d))
 
# This code is contributed by mohit kumar 29


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to return the
// required number X
static int getX(int a, int b, int c, int d)
{
    int X = (b * c - a * d) / (d - c);
    return X;
}
 
// Driver code
static public void Main ()
{
    int a = 2, b = 3, c = 4, d = 5;
    Console.Write(getX(a, b, c, d));
}
}
 
// The code is contributed by Tushil.


Javascript


输出:
2