Dado un triángulo equilátero de lado a , la tarea es encontrar el hexágono más grande que se puede inscribir en él.
Ejemplos:
Entrada: a = 6
Salida: 2
Entrada: a = 9
Salida: 3
Enfoque: A partir de la figura, está claro que los tres triángulos pequeños también son equiláteros. Entonces tendrán una longitud de lado b = a / 3 donde b es también la longitud del hexágono y a es la longitud del triángulo equilátero dado.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find the side of the // largest hexagon which can be inscribed // within an equilateral triangle #include <bits/stdc++.h> using namespace std; // Function to find the side // of the hexagon float hexagonside(float a) { // Side cannot be negative if (a < 0) return -1; // Side of the hexagon float x = a / 3; return x; } // Driver code int main() { float a = 6; cout << hexagonside(a) << endl; return 0; }
Java
// Java program to find the side of the // largest hexagon which can be inscribed // within an equilateral triangle class CLG { // Function to find the side // of the hexagon static float hexagonside(float a) { // Side cannot be negative if (a < 0) return -1; // Side of the hexagon float x = a / 3; return x; } // Driver code public static void main(String[] args) { float a = 6; System.out.println(hexagonside(a)); } }
Python3
# Python3 program to find the side of the # largest hexagon which can be inscribed # within an eqilateral triangle # function to find the side of the hexagon def hexagonside(a): # Side cannot be negative if a < 0: return -1 # Side of the hexagon x = a // 3 return x # Driver code a = 6 print(hexagonside(a)) # This code is contributed # by Mohit kumar 29
C#
using System; // C# program to find the side of the // largest hexagon which can be inscribed // within an equilateral triangle class CLG { // Function to find the side // of the hexagon static float hexagonside(float a) { // Side cannot be negative if (a < 0) return -1; // Side of the hexagon float x = a / 3; return x; } // Driver code public static void Main() { float a = 6; Console.Write(hexagonside(a)); } }
PHP
<?php // PHP program to find the side of the // largest hexagon which can be inscribed // within an equilateral triangle // Function to find the side // of the hexagon function hexagonside($a) { // Side cannot be negative if ($a < 0) return -1; // Side of the hexagon $x = $a / 3; return $x; } // Driver code $a = 6; echo hexagonside($a) ; // This code is contributed by Ryuga ?>
Javascript
<script> // javascript program to find the side of the // largest hexagon which can be inscribed // within an equilateral triangle // Function to find the side // of the hexagon function hexagonside(a) { // Side cannot be negative if (a < 0) return -1; // Side of the hexagon var x = a / 3; return x; } // Driver code var a = 6; document.write(hexagonside(a)); // This code contributed by Princi Singh </script>
Producción:
2
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