📌  相关文章
📜  如果给出了通过将给定数字与每个数字相加而获得的原始比率和新比率,则两个数字之和

📅  最后修改于: 2021-04-24 04:32:22             🧑  作者: Mango

给定两个未知数的比率a:b 。当两个数字都增加给定的整数x时,比率变为c:d 。任务是找到两个数字的和。
例子:

方法:让数字的总和为S。然后,数字可以是(a * S)/(a + b)(b * S)/(a + b)
现在,如下所示:

(((a * S) / (a + b)) + x) / (((b * S) / (a + b)) + x) = c / d
or ((a * S) + x * (a + b)) / ((b * S) + x * (a + b)) = c / d
So, S = (x * (a + b) * (c - d)) / (ad - bc)

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the sum of numbers
// which are in the ration a:b and after
// adding x to both the numbers
// the new ratio becomes c:d
double sum(double a, double b, double c, double d, double x)
{
    double ans = (x * (a + b) * (c - d)) / ((a * d) - (b * c));
    return ans;
}
 
// Driver code
int main()
{
    double a = 1, b = 2, c = 9, d = 13, x = 5;
     
    cout << sum(a, b, c, d, x);
     
    return 0;
}


Java
// Java implementation of the above approach
class GFG
{
     
    // Function to return the sum of numbers
    // which are in the ration a:b and after
    // adding x to both the numbers
    // the new ratio becomes c:d
    static double sum(double a, double b,
                      double c, double d,
                      double x)
    {
        double ans = (x * (a + b) * (c - d)) /
                         ((a * d) - (b * c));
        return ans;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        double a = 1, b = 2, c = 9, d = 13, x = 5;
         
        System.out.println(sum(a, b, c, d, x));
    }
}
 
// This code is contributed by ihritik


Python3
# Python3 implementation of the approach
 
# Function to return the sum of numbers
# which are in the ration a:b and after
# adding x to both the numbers
# the new ratio becomes c:d
def sum(a, b, c, d, x):
 
    ans = ((x * (a + b) * (c - d)) /
               ((a * d) - (b * c)));
    return ans;
 
# Driver code
if __name__ == '__main__':
 
    a, b, c, d, x = 1, 2, 9, 13, 5;
     
    print(sum(a, b, c, d, x));
     
# This code is contributed by PrinciRaj1992


C#
// C# implementation of the above approach
using System;
 
class GFG
{
     
    // Function to return the sum of numbers
    // which are in the ration a:b and after
    // adding x to both the numbers
    // the new ratio becomes c:d
    static double sum(double a, double b,
                      double c, double d,
                      double x)
    {
        double ans = (x * (a + b) * (c - d)) /
                         ((a * d) - (b * c));
        return ans;
    }
     
    // Driver code
    public static void Main ()
    {
        double a = 1, b = 2,
               c = 9, d = 13, x = 5;
         
        Console.WriteLine(sum(a, b, c, d, x));
    }
}
 
// This code is contributed by ihritik


Javascript


输出:
12