Dados dos enteros n , m . Encuentra el número de rectángulos de tamaño 2*1 que se pueden colocar dentro de un rectángulo de tamaño n*m.
Nota:
- No se superponen dos rectángulos pequeños.
- Cada rectángulo pequeño se encuentra completamente dentro del rectángulo grande. Se permite tocar los bordes del rectángulo grande.
Ejemplos :
Input : n = 3, m =3 Output : 4 Input : n = 2, m = 4 Output : 4
Acercarse:
- Si N es par, coloque M filas de N/2 rectángulos pequeños y cubra todo el rectángulo grande.
- Si M es par, coloque N filas de M/2 rectángulos pequeños y cubra todo el rectángulo grande.
- Si ambos son impares, cubra N – 1 fila del tablero con pequeños rectángulos y coloque pequeños rectángulos de piso (M/2) en la última fila. En el peor de los casos (N y M son impares), una celda permanece descubierta.
A continuación se muestra la implementación del enfoque anterior:
C++
// CPP program to Find the number of // rectangles of size 2*1 can be placed // inside a rectangle of size n*m #include <bits/stdc++.h> using namespace std; // function to Find the number of // rectangles of size 2*1 can be placed // inside a rectangle of size n*m int NumberOfRectangles(int n, int m) { // if n is even if (n % 2 == 0) return (n / 2) * m; // if m is even else if (m % 2 == 0) return (m / 2) * n; // if both are odd return (n * m - 1) / 2; } // Driver code int main() { int n = 3, m = 3; // function call cout << NumberOfRectangles(n, m); return 0; }
C
// C program to Find the number of // rectangles of size 2*1 can be placed // inside a rectangle of size n*m #include <stdio.h> // function to Find the number of // rectangles of size 2*1 can be placed // inside a rectangle of size n*m int NumberOfRectangles(int n, int m) { // if n is even if (n % 2 == 0) return (n / 2) * m; // if m is even else if (m % 2 == 0) return (m / 2) * n; // if both are odd return (n * m - 1) / 2; } // Driver code int main() { int n = 3, m = 3; // function call printf("%d",NumberOfRectangles(n, m)); return 0; } // This code is contributed by kothavvsaakash.
Java
// Java program to Find the number of // rectangles of size 2*1 can be placed // inside a rectangle of size n*m public class GFG { // function to Find the number of // rectangles of size 2*1 can be placed // inside a rectangle of size n*m static int NumberOfRectangles(int n, int m) { // if n is even if (n % 2 == 0) return (n / 2) * m; // if m is even else if (m % 2 == 0) return (m / 2) * n; // if both are odd return (n * m - 1) / 2; } public static void main(String args[]) { int n = 3, m = 3; // function call System.out.println(NumberOfRectangles(n, m)); } // This Code is contributed by ANKITRAI1 }
Python3
# Python 3 program to Find the # number of rectangles of size # 2*1 can be placed inside a # rectangle of size n*m # function to Find the number # of rectangles of size 2*1 # can be placed inside a # rectangle of size n*m def NumberOfRectangles(n, m): # if n is even if (n % 2 == 0): return (n / 2) * m # if m is even elif (m % 2 == 0): return (m // 2) * n # if both are odd return (n * m - 1) // 2 # Driver code if __name__ == "__main__": n = 3 m = 3 # function call print(NumberOfRectangles(n, m)) # This code is contributed # by ChitraNayal
C#
// C# program to Find the number of // rectangles of size 2*1 can be placed // inside a rectangle of size n*m using System; class GFG { // function to Find the number of // rectangles of size 2*1 can be placed // inside a rectangle of size n*m static int NumberOfRectangles(int n, int m) { // if n is even if (n % 2 == 0) return (n / 2) * m; // if m is even else if (m % 2 == 0) return (m / 2) * n; // if both are odd return (n * m - 1) / 2; } // Driver Code public static void Main() { int n = 3, m = 3; // function call Console.WriteLine(NumberOfRectangles(n, m)); } // This code is contributed // by Akanksha Rai(Abby_akku) }
PHP
<?php // PHP program to Find the number of // rectangles of size 2*1 can be placed // inside a rectangle of size n*m // function to Find the number of // rectangles of size 2*1 can be placed // inside a rectangle of size n*m function NumberOfRectangles($n, $m) { // if n is even if ($n % 2 == 0) return ($n / 2) * $m; // if m is even else if ($m % 2 == 0) return ($m / 2) * $n; // if both are odd return ($n * $m - 1) / 2; } // Driver code $n = 3; $m = 3; // function call echo NumberOfRectangles($n, $m); // This code is contributed // by Shivi_Aggarwal ?>
Javascript
<script> // Javascript program to Find the number of // rectangles of size 2*1 can be placed // inside a rectangle of size n*m // Function to Find the number of // rectangles of size 2*1 can be placed // inside a rectangle of size n*m function NumberOfRectangles(n, m) { // If n is even if (n % 2 == 0) return (n / 2) * m; // If m is even else if (m % 2 == 0) return (m / 2) * n; // If both are odd return (n * m - 1) / 2; } // Driver Code var n = 3, m = 3; // Function call document.write(NumberOfRectangles(n, m)); // This code is contributed by Ankita saini </script>
Producción
4
Tiempo Complejidad: O(1)
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