📜  求六边形对角线的长度

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

给定一个边长a 的正六边形,任务是找到它对角线的长度。
例子

Input : a = 4
Output : 8

Input : a = 7
Output : 14

从图中可以看出三角形ABC是一个等边三角形,所以
AB = AC = BC = a
也很明显,对角线 = 2*AC2*BC
所以六边形的对角线长度= 2*a
下面是上述方法的实现:

C++
// C++ Program to find the diagonal
// of the hexagon
#include 
using namespace std;
 
// Function to find the diagonal
// of the hexagon
float hexadiagonal(float a)
{
    // side cannot be negative
    if (a < 0)
        return -1;
 
    // diagonal of the hexagon
    return 2 * a;
}
 
// Driver code
int main()
{
    float a = 4;
 
    cout << hexadiagonal(a) << endl;
 
    return 0;
}


Java
// Java Program to find the diagonal
// of the hexagon
 
import java.io.*;
 
class GFG {
 
// Function to find the diagonal
// of the hexagon
static float hexadiagonal(float a)
{
    // side cannot be negative
    if (a < 0)
        return -1;
 
    // diagonal of the hexagon
    return 2 * a;
}
 
// Driver code
 
 
    public static void main (String[] args) {
            float a = 4;
 
    System.out.print( hexadiagonal(a));
}
}
 
// This code is contributed
// by shs


Python3
# Python3 Program to find the diagonal
# of the hexagon
 
# Function to find the diagonal
# of the hexagon
def hexadiagonal(a):
 
    # side cannot be negative
    if (a < 0):
        return -1
 
    # diagonal of the hexagon
    return 2 * a
 
 
# Driver code
if __name__=='__main__':
    a = 4
    print(hexadiagonal(a))
 
# This code is contributed by
# Shivi_Aggarwal


C#
// C# Program to find the diagonal
// of the hexagon
using System;
 
class GFG {
 
    // Function to find the diagonal
    // of the hexagon
    static float hexadiagonal(float a)
    {
        // side cannot be negative
        if (a < 0)
            return -1;
         
        // diagonal of the hexagon
        return 2 * a;
    }
 
// Driver code
public static void Main()
{
   float a = 4;
   Console.WriteLine( hexadiagonal(a));
}
}
 
// This code is contributed
// by anuj_67..


PHP


Javascript


输出:
8

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