给定两个未知数的比率a:b 。当两个数字都增加给定的整数x时,比率变为c:d 。任务是找到两个数字的和。
例子:
Input: a = 2, b = 3, c = 8, d = 9, x = 6
Output: 5
Original numbers are 2 and 3
Original ratio = 2:3
After adding 6, ratio becomes 8:9
2 + 3 = 5
Input: a = 1, b = 2, c = 9, d = 13, x = 5
Output: 12
方法:让数字的总和为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