Dados dos números enteros A y B , que representan el largo y el ancho de una hoja, la tarea es encontrar el número máximo de hojas que se pueden generar reduciendo repetidamente el área a la mitad hasta que no sea divisible por 2 .
Ejemplos:
Entrada : A = 5, B = 10
Salida : 2
Explicación:
- Área Inicial = 5 * 10. Cuenta = 0.
- Área / 2 = 5 * 5. Cuenta = 2.
Entrada : A = 1, B = 8
Salida : 8
Enfoque: siga los pasos a continuación para resolver el problema:
- Calcular el área total de la hoja inicial proporcionada.
- Ahora, sigue dividiendo el área de la hoja por 2 hasta que se vuelva impar.
- Después de cada división, aumente la cuenta al doble de su valor.
- Finalmente, imprima el conteo obtenido.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to calculate the // maximum number of sheets // possible by given operations int maxSheets(int A, int B) { int area = A * B; // Initial count of sheets int count = 1; // Keep dividing the // sheets into half while (area % 2 == 0) { // Reduce area by half area /= 2; // Increase count by twice count *= 2; } return count; } // Driver Code int main() { int A = 5, B = 10; cout << maxSheets(A, B); return 0; }
Java
// Java program for the above approach import java.util.*; class GFG { // Function to calculate the // maximum number of sheets // possible by given operations static int maxSheets(int A, int B) { int area = A * B; // Initial count of sheets int count = 1; // Keep dividing the // sheets into half while (area % 2 == 0) { // Reduce area by half area /= 2; // Increase count by twice count *= 2; } return count; } // Driver Code public static void main(String args[]) { int A = 5, B = 10; System.out.println(maxSheets(A, B)); } } // This code is contributed by jana_sayantan.
Python3
# Python program for the above approach # Function to calculate the # maximum number of sheets # possible by given operations def maxSheets( A, B): area = A * B # Initial count of sheets count = 1 # Keep dividing the # sheets into half while (area % 2 == 0): # Reduce area by half area //= 2 # Increase count by twice count *= 2 return count # Driver Code A = 5 B = 10 print(maxSheets(A, B)) # This code is contributed by rohitsingh07052.
C#
// C# program for the above approach using System; class GFG { // Function to calculate the // maximum number of sheets // possible by given operations static int maxSheets(int A, int B) { int area = A * B; // Initial count of sheets int count = 1; // Keep dividing the // sheets into half while (area % 2 == 0) { // Reduce area by half area /= 2; // Increase count by twice count *= 2; } return count; } // Driver Code public static void Main() { int A = 5, B = 10; Console.WriteLine(maxSheets(A, B)); } } // This code is contributed by chitranayal.
Javascript
<script> // Javascript program for the above approach // Function to calculate the // maximum number of sheets // possible by given operations function maxSheets(A, B) { let area = A * B; // Initial count of sheets let count = 1; // Keep dividing the // sheets into half while (area % 2 == 0) { // Reduce area by half area /= 2; // Increase count by twice count *= 2; } return count; } // Driver Code let A = 5, B = 10; document.write(maxSheets(A, B)); </script>
Producción:
2
Complejidad de Tiempo: O(log 2 (A * B))
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por bhavneet2000 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA