Dados a:b y b:c. La tarea es escribir un programa para encontrar la razón a:b:c
Ejemplos:
Input: a:b = 2:3, b:c = 3:4 Output: 2:3:4 Input: a:b = 3:4, b:c = 8:9 Output: 6:8:9
Enfoque: El truco consiste en hacer que el término común ‘b’ sea igual en ambas proporciones. Por tanto, multiplica la primera razón por b 2 (término b de la segunda razón) y la segunda razón por b 1 .
Dado: a:b 1 y b 2 :c
Solución: a:b:c = (a*b 2 ):(b 1 *b 2 ):(c*b 1 )
Por ejemplo:
Si a : b = 5 : 9 y b : c = 7 : 4, luego encuentre a : b : c.
Solución:
Aquí, haz que el término común ‘b’ sea igual en ambas proporciones.
Por lo tanto, multiplica la primera razón por 7 y la segunda razón por 9.
Entonces, a : b = 35 : 63 y b : c = 63 : 36
Por lo tanto, a : b : c = 35 : 63 : 36
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of above approach #include <bits/stdc++.h> using namespace std; // Function to print a:b:c void solveProportion(int a, int b1, int b2, int c) { int A = a * b2; int B = b1 * b2; int C = b1 * c; // To print the given proportion // in simplest form. int gcd = __gcd(__gcd(A, B), C); cout << A / gcd << ":" << B / gcd << ":" << C / gcd; } // Driver code int main() { // Get the ratios int a, b1, b2, c; // Get ratio a:b1 a = 3; b1 = 4; // Get ratio b2:c b2 = 8; c = 9; // Find the ratio a:b:c solveProportion(a, b1, b2, c); return 0; }
Java
// Java implementation of above approach import java.util.*; import java.lang.*; import java.io.*; class GFG{ static int __gcd(int a,int b){ return b==0 ? a : __gcd(b, a%b); } // Function to print a:b:c static void solveProportion(int a, int b1, int b2, int c) { int A = a * b2; int B = b1 * b2; int C = b1 * c; // To print the given proportion // in simplest form. int gcd = __gcd(__gcd(A, B), C); System.out.print( A / gcd + ":" + B / gcd + ":" + C / gcd); } // Driver code public static void main(String args[]) { // Get the ratios int a, b1, b2, c; // Get ratio a:b1 a = 3; b1 = 4; // Get ratio b2:c b2 = 8; c = 9; // Find the ratio a:b:c solveProportion(a, b1, b2, c); } }
Python 3
# Python 3 implementation # of above approach import math # Function to print a:b:c def solveProportion(a, b1, b2, c): A = a * b2 B = b1 * b2 C = b1 * c # To print the given proportion # in simplest form. gcd1 = math.gcd(math.gcd(A, B), C) print( str(A // gcd1) + ":" + str(B // gcd1) + ":" + str(C // gcd1)) # Driver code if __name__ == "__main__": # Get ratio a:b1 a = 3 b1 = 4 # Get ratio b2:c b2 = 8 c = 9 # Find the ratio a:b:c solveProportion(a, b1, b2, c) # This code is contributed # by ChitraNayal
C#
// C# implementation of above approach using System; class GFG { static int __gcd(int a,int b) { return b == 0 ? a : __gcd(b, a % b); } // Function to print a:b:c static void solveProportion(int a, int b1, int b2, int c) { int A = a * b2; int B = b1 * b2; int C = b1 * c; // To print the given proportion // in simplest form. int gcd = __gcd(__gcd(A, B), C); Console.Write( A / gcd + ":" + B / gcd + ":" + C / gcd); } // Driver code public static void Main() { // Get the ratios int a, b1, b2, c; // Get ratio a:b1 a = 3; b1 = 4; // Get ratio b2:c b2 = 8; c = 9; // Find the ratio a:b:c solveProportion(a, b1, b2, c); } } // This code is contributed // by Akanksha Rai(Abby_akku)
PHP
<?php // PHP implementation of above approach function __gcd($a, $b) { return $b == 0 ? $a : __gcd($b, $a % $b); } // Function to print a:b:c function solveProportion($a, $b1, $b2, $c) { $A = $a * $b2; $B = $b1 * $b2; $C = $b1 * $c; // To print the given proportion // in simplest form. $gcd = __gcd(__gcd($A, $B), $C); echo ($A / $gcd) . ":" . ($B / $gcd) . ":" . ($C / $gcd); } // Driver code // Get the ratios // Get ratio a:b1 $a = 3; $b1 = 4; // Get ratio b2:c $b2 = 8; $c = 9; // Find the ratio a:b:c solveProportion($a, $b1, $b2, $c); // This code is contributed by mits ?>
Javascript
<script> // Javascript implementation of above approach function __gcd(a, b) { return b == 0 ? a : __gcd(b, a % b); } // Function to print a:b:c function solveProportion(a, b1, b2, c) { let A = a * b2; let B = b1 * b2; let C = b1 * c; // To print the given proportion // in simplest form. let gcd = __gcd(__gcd(A, B), C); document.write( A / gcd + ":" + B / gcd + ":" + C / gcd); } // Get the ratios let a, b1, b2, c; // Get ratio a:b1 a = 3; b1 = 4; // Get ratio b2:c b2 = 8; c = 9; // Find the ratio a:b:c solveProportion(a, b1, b2, c); // This code is contributed by divyeshrabadiya07. </script>
6:8:9
Complejidad de tiempo: O(log(A+B)), donde A=a*b2 y B = b1*b2
Complejidad espacial : O(1)
Publicación traducida automáticamente
Artículo escrito por Smitha Dinesh Semwal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA