给定六边形的对角线长度 d。你的任务是找到那个六边形的面积。
例子:
Input : 5
Output : Area of Hexagon: 16.238
Input : 10
Output : Area of Hexagon: 64.9519
六边形
六边形是具有六个相等边和所有相等角度的正多边形。六边形的内角各为120度,六边形所有内角之和为720度。
设 d 为六边形的对角线,则计算六边形面积的公式为
面积 =
上面的公式是如何工作的?
我们知道边长为a = (3 √3(a) 2 ) / 2 的六边形面积。由于所有边的大小和角度都是120 度,因此d = 2a 或a = d/2。输入这个值后,我们得到面积为 (3 √3(d) 2 ) / 8。
下面是实现。
C++
// C++ program to find the area of Hexagon with given diagonal
#include
using namespace std;
// Function to calculate area
float hexagonArea(float d)
{
// Formula to find area
return (3 * sqrt(3) * pow(d, 2)) / 8;
}
// Main
int main()
{
float d = 10;
cout << "Area of hexagon: " << hexagonArea(d);
return 0;
}
Java
// Java program to find the area of
// Hexagon with given diagonal
import java.lang.Math;
public class GfG {
// Function to calculate area
public static float hexagonArea(float d)
{
// Formula to find area
return (float)((3 * Math.sqrt(3) * d * d) / 8);
}
public static void main(String []args) {
float d = 10;
System.out.println("Area of hexagon: " + hexagonArea(d));
}
}
// This code is contributed by Rituraj Jain
Python3
# Python3 program to find the area
# of Hexagon with given diagonal
from math import sqrt
# Function to calculate area
def hexagonArea(d) :
# Formula to find area
return (3 * sqrt(3) * pow(d, 2)) / 8
# Driver ode
if __name__ == "__main__" :
d = 10
print("Area of hexagon:",
round(hexagonArea(d), 3))
# This code is contributed by Ryuga
C#
// C# program to find the area of
// Hexagon with given diagonal
using System;
public class GFG{
// Function to calculate area
public static float hexagonArea(float d)
{
// Formula to find area
return (float)((3 * Math.Sqrt(3) * d * d) / 8);
}
//Code driven
static public void Main (){
float d = 10;
Console.WriteLine("Area of hexagon: " + hexagonArea(d));
}
//This code is contributed by Tushil.
}
PHP
Javascript
输出:
Area of Hexagon: 64.952
时间复杂度: O(1)