一只蜥蜴出现在立方体的一个角上,它想要到达立方体对角的对角。您必须计算蜥蜴到达目的地所必须覆盖的最小距离。
注意:蜥蜴不会飞,它会沿着墙壁移动。
您将得到一个代表立方体的面。您必须计算蜥蜴必须经过的最小距离。
例子:
Input : 5
Output :11.1803
Input :2
Output :4.47214
由于我们必须计算从一个角到另一个对角相对角的最小距离。如果蜥蜴能够飞行,那么最短的距离将是对角线的长度。但是不能。
因此,要计算最小距离,只需打开立方体,如图所示即可。
让我们假设,蜥蜴最初在E点,并且必须到达A点(因为A与E对角线对角)。现在我们必须找到AE 。
只需使用毕达哥拉斯定理,如
AC =一个
CE = CD + DE = 2a
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