Dado un número entero A , que denota la longitud de un cubo, la tarea es encontrar la distancia más corta entre la diagonal de un cubo y un borde sesgado, es decir, KL en la figura a continuación.
Ejemplos:
Entrada: A = 2
Salida: 1,4142
Explicación:
Longitud de KL = A / √2
Longitud de KL = 2 / 1,41 = 1,4142Entrada: A = 3
Salida: 2.1213
Planteamiento: La idea para resolver el problema se basa en la siguiente fórmula matemática:
Dibujemos una perpendicular desde K hacia la cara inferior del cubo como Q.
Usando el teorema de Pitágoras en el triángulo QKL,KL 2 = QK 2 + QL 2
l 2 = (a/2) 2 + (a/2) 2
l 2 = 2 * (a/2) 2
l = a / sqrt(2)
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the shortest distance // between the diagonal of a cube and // an edge skew to it float diagonalLength(float a) { // Stores the required distance float L = a / sqrt(2); // Print the required distance cout << L; } // Driver Code int main() { // Given side of the cube float a = 2; // Function call to find the shortest // distance between the diagonal of a // cube and an edge skew to it diagonalLength(a); return 0; }
Java
// Java program for the above approach class GFG { // Function to find the shortest // distance between the diagonal of a // cube and an edge skew to it static void diagonalLength(float a) { // Stores the required distance float L = a / (float)Math.sqrt(2); // Print the required distance System.out.println(L); } // Driver Code public static void main(String[] args) { // Given side of the cube float a = 2; // Function call to find the shortest // distance between the diagonal of a // cube and an edge skew to it diagonalLength(a); } }
Python3
# Python3 program for the above approach from math import sqrt # Function to find the shortest # distance between the diagonal of a # cube and an edge skew to it def diagonalLength(a): # Stores the required distance L = a / sqrt(2) # Print the required distance print(L) # Given side of the cube a = 2 # Function call to find the shortest # distance between the diagonal of a # cube and an edge skew to it diagonalLength(a)
C#
// C# program for the above approach using System; class GFG { // Function to find the shortest // distance between the diagonal of a // cube and an edge skew to it static void diagonalLength(float a) { // Stores the required distance float L = a / (float)Math.Sqrt(2); // Print the required distance Console.Write(L); } // Driver Code public static void Main() { // Given side of the cube float a = 2; // Function call to find the shortest // distance between the diagonal of a // cube and an edge skew to it diagonalLength(a); } }
PHP
<?php // PHP program for the above approach // Function to find the shortest // distance between the diagonal of a // cube and an edge skew to it function diagonalLength($a) { // Stores the required distance $L = $a / sqrt(2); # Print the required distance echo $L; } // Given side of the cube $a = 2; // Function call to find the shortest // distance between the diagonal of a // cube and an edge skew to it diagonalLength($a); ?>
Javascript
<script> // javascript program for the above approach // Function to find the shortest distance // between the diagonal of a cube and // an edge skew to it function diagonalLength( a) { // Stores the required distance let L = a / Math.sqrt(2); // Print the required distance document.write( L.toFixed(5)); } // Driver Code // Given side of the cube let a = 2; // Function call to find the shortest // distance between the diagonal of a // cube and an edge skew to it diagonalLength(a); // This code is contributed by todaysgaurav </script>
1.41421
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por thotasravya28 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA