Dado el lado de un cuadrado a , la tarea es encontrar el lado del hexágono más grande que se puede inscribir dentro del cuadrado dado.
Ejemplos:
Entrada: a = 6
Salida: 3,1056
Entrada: a = 8
Salida: 4,1408
Enfoque: Deje que el lado del hexágono sea x y suponga que el lado del cuadrado, a se divide en una longitud más pequeña b y una longitud más grande c , es decir , a = b + c
Ahora, a partir de la figura, vemos,
b 2 + b 2 = x 2 lo que da b = x / √2
Ahora nuevamente, d / (2 * x) = cos(30) = √3 / 2 es decir , d = x√3
Y, c 2 + c 2 = d 2 lo que da c = d / √2 = x√3 / √2
Ya que, a = b + c . Entonces, a = x / √2 + x√3 / √2 = ((1 + √3) / √2) * x = 1.932 * x
Entonces, lado del hexágono, x = 0.5176 * a
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ Program to find the biggest hexagon which // can be inscribed within the given square #include <bits/stdc++.h> using namespace std; // Function to return the side // of the hexagon float hexagonside(float a) { // Side cannot be negative if (a < 0) return -1; // Side of the hexagon float x = 0.5176 * a; return x; } // Driver code int main() { float a = 6; cout << hexagonside(a) << endl; return 0; }
Java
// Java Program to find the biggest hexagon which // can be inscribed within the given square import java.io.*; class GFG { // Function to return the side // of the hexagon static double hexagonside(double a) { // Side cannot be negative if (a < 0) return -1; // Side of the hexagon double x = (0.5176 * a); return x; } // Driver code public static void main (String[] args) { double a = 6; System.out.println (hexagonside(a)); } //This code is contributed by ajit. }
Python 3
# Python 3 Program to find the biggest # hexagon which can be inscribed within # the given square # Function to return the side # of the hexagon def hexagonside(a): # Side cannot be negative if (a < 0): return -1; # Side of the hexagon x = 0.5176 * a; return x; # Driver code a = 6; print(hexagonside(a)); # This code is contributed # by Akanksha Rai
C#
// C# Program to find the biggest hexagon which // can be inscribed within the given square using System; class GFG { // Function to return the side // of the hexagon static double hexagonside(double a) { // Side cannot be negative if (a < 0) return -1; // Side of the hexagon double x = (0.5176 * a); return x; } // Driver code public static void Main () { double a = 6; Console.WriteLine(hexagonside(a)); } } // This code is contributed by Ryuga.
PHP
<?php // PHP Program to find the biggest hexagon which // can be inscribed within the given square // Function to return the side of the hexagon function hexagonside($a) { // Side cannot be negative if ($a < 0) return -1; // Side of the hexagon $x = 0.5176 * $a; return $x; } // Driver code $a = 6; echo hexagonside($a); // This code is contributed by akt_mit ?>
Javascript
<script> // Javascript Program to find the biggest hexagon which // can be inscribed within the given square // Function to return the side // of the hexagon function hexagonside(a) { // Side cannot be negative if (a < 0) return -1; // Side of the hexagon let x = 0.5176 * a; return x; } // Driver code let a = 6; document.write(hexagonside(a) + "<br>"); // This code is contributed by Manoj </script>
3.1056
Complejidad de tiempo: 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