Aquí se da un triángulo equilátero de lado de longitud a , que inscribe un hexágono que a su vez inscribe un cuadrado. La tarea es encontrar la longitud del lado del cuadrado.
Ejemplos:
Input: a = 6 Output: 2.538 Input: a = 8 Output: 3.384
Enfoque :
Sabemos que la longitud del lado de un hexágono inscrito dentro de un triángulo equilátero es h = a/3 . Consulte el hexágono más grande que se puede inscribir dentro de un triángulo equilátero .
Además, la longitud del lado del cuadrado que se puede inscribir dentro de un hexágono es x = 1.268h Consulte el cuadrado más grande que se puede inscribir dentro de un hexágono .
Entonces, la longitud del lado del cuadrado inscrito dentro de un hexágono que a su vez está inscrito dentro de un triángulo equilátero, x = 0.423a .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find the side of the largest square // that can be inscribed within the hexagon which in return // is incsribed within an equilateral triangle #include <bits/stdc++.h> using namespace std; // Function to find the side // of the square float squareSide(float a) { // Side cannot be negative if (a < 0) return -1; // side of the square float x = 0.423 * a; return x; } // Driver code int main() { float a = 8; cout << squareSide(a) << endl; return 0; }
Java
// Java program to find the side of the // largest square that can be inscribed // within the hexagon which in return is // incsribed within an equilateral triangle class cfg { // Function to find the side // of the square static float squareSide(float a) { // Side cannot be negative if (a < 0) return -1; // side of the square float x = (0.423f * a); return x; } // Driver code public static void main(String[] args) { float a = 8; System.out.println(squareSide(a)); } } // This code is contributed by // Mukul Singh.
Python3
# Python 3 program to find the side of the # largest square that can be inscribed # within the hexagon which in return # is incsribed within an equilateral triangle # Function to find the side of the square def squareSide(a): # Side cannot be negative if (a < 0): return -1 # side of the square x = 0.423 * a return x # Driver code if __name__ == '__main__': a = 8 print(squareSide(a)) # This code is contributed by # Sanjit_Prasad
C#
// C# program to find the side of the // largest square that can be inscribed // within the hexagon which in return is // incsribed within an equilateral triangle using System; class GFG { // Function to find the side // of the square static float squareSide(float a) { // Side cannot be negative if (a < 0) return -1; // side of the square float x = (0.423f * a); return x; } // Driver code public static void Main() { float a = 8; Console.WriteLine(squareSide(a)); } } // This code is contributed by // shs
PHP
<?php // PHP program to find the side of the // largest square that can be inscribed // within the hexagon which in return is // incsribed within an equilateral triangle // Function to find the side of the square function squareSide($a) { // Side cannot be negative if ($a < 0) return -1; // side of the square $x = 0.423 * $a; return $x; } // Driver code $a = 8; echo squareSide($a); // This code is contributed by ajit. ?>
Javascript
<script> // javascript program to find the side of the // largest square that can be inscribed // within the hexagon which in return is // incsribed within an equilateral triangle // Function to find the side // of the square function squareSide(a) { // Side cannot be negative if (a < 0) return -1; // side of the square var x = (0.423 * a); return x; } // Driver code var a = 8; document.write(squareSide(a)); // This code is contributed by Princi Singh </script>
3.384
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