📜  拼图 |蜥蜴的最小距离

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

一只蜥蜴出现在立方体的一个角上,它想要到达立方体的对角。您必须计算蜥蜴到达目的地所需的最小距离。
注意:蜥蜴不会飞,它会沿着墙壁移动。
给你一个立方体代表边。你必须计算蜥蜴必须移动的最小距离。
例子:

Input : 5
Output :11.1803

Input :2
Output :4.47214

因为我们必须计算从一个角到另一个对角的最小距离。如果蜥蜴能飞,那么最短的距离就是对角线的长度。但它不能。

图像

因此,要计算最小距离,只需打开立方体,如图所示。
让我们假设,蜥蜴最初在E点。它必须到达A点(因为 A 与 E 对角线相反)。现在我们必须找到AE
只需使用毕达哥拉斯定理,如
AC=a
CE=CD+DE=2a

    $$AE=\sqrt{AC^2+CE^2}$$ $$AE=\sqrt{a^2+\left( 2a \right)^2}$$ $$AE=\sqrt{5a^2}$$

C++
// CPP program to find minimum distance to be travlled
// by lizard.
#include 
#define ll long long int
using namespace std;
int main()
{
    // side of cube
    ll a = 5;
 
    // understand from diagram
    ll AC = a;
 
    // understand from diagram
    ll CE = 2 * a;
 
    // minimum distance
    double shortestDistace = sqrt(AC * AC + CE * CE);
 
    cout << shortestDistace << endl;
    return 0;
}


Java
//Java program to find minimum
//distance to be travelled by lizard
import java.util.*;
 
class solution
{
public static void main(String arr[])
{
    // side of the cube
    int a = 5;
 
    // understand from diagram
    int AC = a;
 
    // understand from diagram
    int CE = 2 * a;
 
    // minimum distance
    double shortestDistace = Math.sqrt(AC * AC + CE * CE);
 
    System.out.println(shortestDistace);
}
}


Python3
# Python3 program to find minimum
# distance to be travelled by lizard
 
import math
 
#side of cube
if __name__=='__main__':
    a = 5
 
#understand from diagram
    AC = a
 
#understand from diagram
    CE = 2 * a
 
#minimum distance
    shortestDistace = math.sqrt(AC * AC + CE * CE)
 
    print(shortestDistace)
 
#this code is Contributed by Shashank_Sharma


C#
// C# program to find minimum
// distance to be travelled by lizard
using System;
 
class GFG
{
public static void Main()
{
    // side of the cube
    int a = 5;
 
    // understand from diagram
    int AC = a;
 
    // understand from diagram
    int CE = 2 * a;
 
    // minimum distance
    double shortestDistace = Math.Sqrt(AC * AC + CE * CE);
 
    Console.Write(shortestDistace);
}
}
 
// This code is contributed by ita_c


PHP


Javascript


输出:
11.1803

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