Dado un rectángulo de lados m y n. Corta el rectángulo en piezas idénticas más pequeñas de modo que cada pieza sea un cuadrado que tenga la máxima longitud de lado posible sin ninguna parte sobrante del rectángulo. Imprime el número de dichos cuadrados formados.
Ejemplos:
Input: 9 6 Output: 6 Rectangle can be cut into squares of size 3. Input: 4 2 Output: 2 Rectangle can be cut into squares of size 2.
Enfoque: La tarea es cortar el rectángulo en cuadrados con el lado de longitud s sin que queden partes del rectángulo, por lo que s debe dividir tanto m como n . Además, el lado del cuadrado debe ser el máximo posible, por lo tanto, s debe ser el máximo común divisor de m y n.
entonces, s = mcd(m, n) .
Para encontrar el número de cuadrados en los que se corta el rectángulo, la tarea a realizar es dividir el área de un rectángulo con un área del cuadrado de tamaño s.
C++
// C++ code for calculating the // number of squares #include <bits/stdc++.h> using namespace std; // Function to find number of squares int NumberOfSquares(int x, int y) { // Here in built c++ gcd function is used int s = __gcd(x, y); int ans = (x * y) / (s * s); return ans; } // Driver code int main() { int m = 385, n = 60; // Call the function NumberOfSquares cout << NumberOfSquares(m, n); return 0; }
C
// C code for calculating the // number of squares #include <stdio.h> int gcd(int a, int b) { int gcd = 1; for(int i = 1; i <= a && i <= b; i++) { if (a % i ==0 && b % i == 0) gcd = i; } return gcd; } // Function to find number of squares int NumberOfSquares(int x, int y) { // Here in built c++ gcd function is used int s = gcd(x, y); int ans = (x * y) / (s * s); return ans; } // Driver code int main() { int m = 385, n = 60; // Call the function NumberOfSquares printf("%d",NumberOfSquares(m, n)); return 0; } // This code is contributed by kothavvsaakash.
Java
// Java code for calculating // the number of squares 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 || b == 0) return 0; // 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 // number of squares static int NumberOfSquares(int x, int y) { // Here in built c++ // gcd function is used int s = __gcd(x, y); int ans = (x * y) / (s * s); return ans; } // Driver Code public static void main (String[] args) { int m = 385, n = 60; // Call the function // NumberOfSquares System.out.println(NumberOfSquares(m, n)); } } // This code is contributed by anuj_67.
Python3
# Python3 code for calculating # the number of squares # Recursive function to # return gcd of a and b def __gcd(a, b): # Everything divides 0 if (a == 0 or b == 0): return 0; # 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 # number of squares def NumberOfSquares(x, y): # Here in built PHP # gcd function is used s = __gcd(x, y); ans = (x * y) / (s * s); return int(ans); # Driver Code m = 385; n = 60; # Call the function # NumberOfSquares print(NumberOfSquares(m, n)); # This code is contributed # by mit
C#
// C# code for calculating // the number of squares 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 || b == 0) return 0; // 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 // number of squares static int NumberOfSquares(int x, int y) { // Here in built c++ // gcd function is used int s = __gcd(x, y); int ans = (x * y) / (s * s); return ans; } // Driver Code static public void Main () { int m = 385, n = 60; // Call the function // NumberOfSquares Console.WriteLine(NumberOfSquares(m, n)); } } // This code is contributed by ajit
PHP
<?php // PHP code for calculating // the number of squares // Recursive function to // return gcd of a and b function __gcd($a, $b) { // Everything divides 0 if ($a == 0 || $b == 0) return 0; // 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 // number of squares function NumberOfSquares($x, $y) { // Here in built PHP // gcd function is used $s = __gcd($x, $y); $ans = ($x * $y) / ($s * $s); return $ans; } // Driver Code $m = 385; $n = 60; // Call the function // NumberOfSquares echo (NumberOfSquares($m, $n)); // This code is contributed // by akt_mit ?>
Javascript
<script> // Javascript code for calculating the // number of squares function __gcd(a, b) { // Everything divides 0 if (a == 0 || b == 0) return 0; // 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 number of squares function NumberOfSquares(x, y) { // Here in built c++ gcd function is used let s = __gcd(x, y); let ans = parseInt((x * y) / (s * s)); return ans; } // Driver code let m = 385, n = 60; // Call the function NumberOfSquares document.write(NumberOfSquares(m, n)); </script>
924
Complejidad del tiempo: O(log(max(m,n))
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por pawan_asipu y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA