Suma de dos números si se da la razón original y la nueva razón obtenida al sumar un número dado a cada número

Dada una razón a : b de dos números desconocidos. Cuando ambos números se incrementan en un número entero dado x , la relación se convierte en c: d . La tarea es encontrar la suma de los dos números.
Ejemplos: 
 

Entrada: a = 2, b = 3, c = 8, d = 9, x = 6 
Salida:
Los números originales son 2 y 3 
Relación original = 2:3 
Después de sumar 6, la relación se convierte en 8:9 
2 + 3 = 5
Entrada: a = 1, b = 2, c = 9, d = 13, x = 5 
Salida: 12 
 

Enfoque: Sea la suma de los números S . Entonces, los números pueden ser (a * S)/(a + b) y (b * S)/(a + b)
Ahora, como se da: 
 

(((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)

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
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

<script>
 
// javascript implementation of the above 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
function sum(a , b, c , d, x)
{
    var ans = (x * (a + b) * (c - d)) /
                     ((a * d) - (b * c));
    return ans;
}
 
// Driver code
 
var a = 1, b = 2, c = 9, d = 13, x = 5;
 
document.write(sum(a, b, c, d, x));
 
 
// This code is contributed by 29AjayKumar
 
</script>
Producción: 

12

 

Complejidad de tiempo: O(1)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por IshwarGupta y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *