Dado un semicírculo de radio r , tenemos que encontrar el rectángulo más grande que se puede inscribir en el semicírculo, con base en el diámetro.
Ejemplos:
Input : r = 4 Output : 16 Input : r = 5 Output :25
Sea r el radio del semicírculo, x la mitad de la base del rectángulo y y la altura del rectángulo. Queremos maximizar el área, A = 2xy.
Entonces, del diagrama que tenemos,
y = √(r^2 – x^2)
Entonces, A = 2*x*(√(r^2 – x^2)) , o dA/dx = 2*√(r ^2 – x^2) -2*x^2/√(r^2 – x^2)
Igualando esta derivada a 0 y resolviendo para x,
dA/dx = 0
o, 2*√(r^2 – x^2) – 2*x^2/√(r^2 – x^2) = 0
2r^2 – 4x^2 = 0
x = r/√2
Este es el máximo del área como,
dA/dx > 0 cuando x > r/√2
y, dA/dx < 0 cuando x > r/√2
Dado quey =√(r^2 – x^2) entonces tenemos
y = r/√2
Por lo tanto, la base del rectángulo tiene una longitud = r/√2 y su altura tiene una longitud √2*r/2 .
Entonces, Área, A=r^2
C++
// C++ Program to find the // the biggest rectangle // which can be inscribed // within the semicircle #include <bits/stdc++.h> using namespace std; // Function to find the area // of the biggest rectangle float rectanglearea(float r) { // the radius cannot be negative if (r < 0) return -1; // area of the rectangle float a = r * r; return a; } // Driver code int main() { float r = 5; cout << rectanglearea(r) << endl; return 0; }
Java
// Java Program to find the // the biggest rectangle // which can be inscribed // within the semicircle class GFG { // Function to find the area // of the biggest rectangle static float rectanglearea(float r) { // the radius cannot be negative if (r < 0) return -1; // area of the rectangle float a = r * r; return a; } // Driver code public static void main(String[] args) { float r = 5; System.out.println((int)rectanglearea(r)); } } // This code is contributed // by ChitraNayal
Python 3
# Python 3 Program to find the # the biggest rectangle # which can be inscribed # within the semicircle # Function to find the area # of the biggest rectangle def rectanglearea(r) : # the radius cannot # be negative if r < 0 : return -1 # area of the rectangle a = r * r return a # Driver Code if __name__ == "__main__" : r = 5 # function calling print(rectanglearea(r)) # This code is contributed # by ANKITRAI1
C#
// C# Program to find the // the biggest rectangle // which can be inscribed // within the semicircle using System; class GFG { // Function to find the area // of the biggest rectangle static float rectanglearea(float r) { // the radius cannot be negative if (r < 0) return -1; // area of the rectangle float a = r * r; return a; } // Driver code public static void Main() { float r = 5; Console.Write((int)rectanglearea(r)); } } // This code is contributed // by ChitraNayal
PHP
<?php // PHP Program to find the // the biggest rectangle // which can be inscribed // within the semicircle // Function to find the area // of the biggest rectangle function rectanglearea($r) { // the radius cannot // be negative if ($r < 0) return -1; // area of the rectangle $a = $r * $r; return $a; } // Driver code $r = 5; echo rectanglearea($r)."\n"; // This code is contributed // by ChitraNayal ?>
Javascript
<script> // javascript Program to find the // the biggest rectangle // which can be inscribed // within the semicircle // Function to find the area // of the biggest rectangle function rectanglearea(r) { // the radius cannot be negative if (r < 0) return -1; // area of the rectangle var a = r * r; return a; } // Driver code var r = 5; document.write(parseInt(rectanglearea(r))); // This code is contributed by Amit Katiyar </script>
PRODUCCIÓN :
25
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por IshwarGupta y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA