Dados dos rectángulos, X con una razón de largo a ancho a:b e Y con una razón de largo a ancho c:d respectivamente. Ambos rectángulos se pueden cambiar de tamaño siempre que la proporción de los lados siga siendo la misma. La tarea es colocar el segundo rectángulo dentro del primer rectángulo de modo que al menos 1 lado sea igual y ese lado se superponga a ambos rectángulos y encuentre la proporción de (espacio ocupado por un segundo rectángulo): (espacio ocupado por el primer rectángulo) .
Ejemplos:
Input: a = 1, b = 1, c = 3, d = 2 Output: 2:3 The dimensions can be 3X3 and 3X2. Input: a = 4, b = 3, c = 2, d = 2 Output: 3:4 The dimensions can be 4X3 and 3X3
Enfoque: Si hacemos que uno de los lados de los rectángulos sea igual, entonces la razón requerida sería la razón del otro lado.
Considere 2 casos:
- a*d < b*c : Debemos igualar a y c.
- b*c < a*d : Debemos igualar b y d.
Ya que multiplicar ambos lados de una razón no cambia su valor. Primero intenta igualar a y c, se puede hacer igual a sus mcm multiplicando (a:b) por mcm/a y (c:d) por mcm/c. Después de la multiplicación, la razón de (b:d) será la respuesta requerida. Esta relación se puede reducir dividiendo byd con mcd(b, d).
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 find the ratio void printRatio(int a, int b, int c, int d) { if (b * c > a * d) { swap(c, d); swap(a, b); } // LCM of numerators int lcm = (a * c) / __gcd(a, c); int x = lcm / a; b *= x; int y = lcm / c; d *= y; // Answer in reduced form int k = __gcd(b, d); b /= k; d /= k; cout << b << ":" << d; } // Driver code int main() { int a = 4, b = 3, c = 2, d = 2; printRatio(a, b, c, d); return 0; }
Java
// Java implementation of above approach import java.io.*; class GFG { // Recursive function to return gcd of a and b static int __gcd(int a, int b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return __gcd(a-b, b); return __gcd(a, b-a); } // Function to find the ratio static void printRatio(int a, int b, int c, int d) { if (b * c > a * d) { int temp = c; c =d; d =c; temp =a; a =b; b=temp; } // LCM of numerators int lcm = (a * c) / __gcd(a, c); int x = lcm / a; b *= x; int y = lcm / c; d *= y; // Answer in reduced form int k = __gcd(b, d); b /= k; d /= k; System.out.print( b + ":" + d); } // Driver code public static void main (String[] args) { int a = 4, b = 3, c = 2, d = 2; printRatio(a, b, c, d); } } // This code is contributed by inder_verma..
Python3
import math # Python3 implementation of above approach # Function to find the ratio def printRatio(a, b, c, d): if (b * c > a * d): swap(c, d) swap(a, b) # LCM of numerators lcm = (a * c) / math.gcd(a, c) x = lcm / a b = int(b * x) y = lcm / c d = int(d * y) # Answer in reduced form k = math.gcd(b,d) b =int(b / k) d = int(d / k) print(b,":",d) # Driver code if __name__ == '__main__': a = 4 b = 3 c = 2 d = 2 printRatio(a, b, c, d) # This code is contributed by # Surendra_Gangwar
C#
// C# implementation of above approach using System; class GFG { // Recursive function to return gcd of a and b static int __gcd(int a, int b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return __gcd(a-b, b); return __gcd(a, b-a); } // Function to find the ratio static void printRatio(int a, int b, int c, int d) { if (b * c > a * d) { int temp = c; c =d; d =c; temp =a; a =b; b=temp; } // LCM of numerators int lcm = (a * c) / __gcd(a, c); int x = lcm / a; b *= x; int y = lcm / c; d *= y; // Answer in reduced form int k = __gcd(b, d); b /= k; d /= k; Console.WriteLine( b + ":" + d); } // Driver code public static void Main () { int a = 4, b = 3, c = 2, d = 2; printRatio(a, b, c, d); } } // This code is contributed by inder_verma..
PHP
<?php // PHP implementation of above approach // Recursive function to return // gcd of a and b function __gcd($a, $b) { // Everything divides 0 if ($a == 0) return $b; if ($b == 0) return $a; // base case if ($a == $b) return $a; // a is greater if ($a > $b) return __gcd($a - $b, $b); return __gcd($a, $b - $a); } // Function to find the ratio function printRatio($a, $b, $c, $d) { if ($b * $c > $a * $d) { $temp = $c; $c = $d; $d = $c; $temp = $a; $a = $b; $b = $temp; } // LCM of numerators $lcm = ($a * $c) / __gcd($a, $c); $x = $lcm / $a; $b *= $x; $y = $lcm / $c; $d *= $y; // Answer in reduced form $k = __gcd($b, $d); $b /= $k; $d /= $k; echo $b . ":" . $d; } // Driver code $a = 4; $b = 3; $c = 2; $d = 2; printRatio($a, $b, $c, $d); // This code is contributed // by Akanksha Rai ?>
Javascript
<script> // Javascript implementation of above approach // Recursive function to return // gcd of a and b function __gcd(a, b) { // Everything divides 0 if (a == 0) return b; if (b == 0) return a; // base case if (a == b) return a; // a is greater if (a > b) return __gcd(a - b,b); return __gcd(a, b - a); } // Function to find the ratio function printRatio(a, b, c, d) { if (b * c > a * d) { temp = c; c = d; d = c; temp = a; a = b; b = temp; } // LCM of numerators let lcm = (a * c) / __gcd(a, c); let x = lcm / a; b *= x; let y = lcm / c; d *= y; // Answer in reduced form let k = __gcd(b, d); b /= k; d /= k; document.write(b + ":" + d); } // Driver code let a = 4, b = 3, c = 2, d = 2; printRatio(a, b, c, d); // This code is contributed by Manoj </script>
3:4
Complejidad del tiempo: O(log(max(a,c))+log(max(b,d)))
Espacio auxiliar: O(log(max(a,c))+log(max(b,d)))
Publicación traducida automáticamente
Artículo escrito por Abdullah Aslam y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA