📜  寻找立方体对角线和边缘倾斜之间最短距离的程序

📅  最后修改于: 2021-10-23 08:10:04             🧑  作者: Mango

给定一个整数A ,表示立方体的长度,任务是找到立方体的对角线和向它倾斜的边之间的最短距离,即下图中的KL

例子 :

方法:解决问题的思路是基于以下数学公式:

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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


Javascript


输出:
1.41421

时间复杂度: O(1)
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程