📜  六边形可内接的最大正方形

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

给定一个边长a 的正六边形,任务是找到可以内接的最大正方形的面积。
例子:

方法:我们将推导出的正方形将具有相同的六边形中心和轴。这是因为如果我们旋转它,正方形会变小。

下面是上述方法的实现:

C++
// C++ program to find the area of the largest square
// that can be inscribed within the hexagon
#include 
using namespace std;
 
// Function to find the area
// of the square
float squareArea(float a)
{
 
    // Side cannot be negative
    if (a < 0)
        return -1;
 
    // Area of the square
    float area = pow(1.268, 2) * pow(a, 2);
    return area;
}
 
// Driver code
int main()
{
    float a = 6;
    cout << squareArea(a) << endl;
    return 0;
}


Java
// Java program to find the area of the largest square
// that can be inscribed within the hexagon
class Solution {
    // Function to find the area
    // of the square
    static float squareArea(float a)
    {
 
        // Side cannot be negative
        if (a < 0)
            return -1;
 
        // Area of the square
        float area = (float)(Math.pow(1.268, 2) * Math.pow(a, 2));
        return area;
    }
 
    // Driver code
    public static void main(String args[])
    {
        float a = 6;
        System.out.println(squareArea(a));
    }
}
 
// This code is contributed by Arnab Kundu


Python3
# Python program to find the area of the largest square
# that can be inscribed within the hexagon
 
# Function to find the area
# of the square
def squareArea(a):
 
    # Side cannot be negative
    if (a < 0):
        return -1;
 
    # Area of the square
    area = (1.268 ** 2) * (a ** 2);
    return area;
 
# Driver code
a = 6;
print(squareArea(a));
 
# This code contributed by PrinciRaj1992


C#
// C# program to find the area of the largest square
// that can be inscribed within the hexagon
using System;
class Solution {
    // Function to find the area
    // of the square
    static float squareArea(float a)
    {
 
        // Side cannot be negative
        if (a < 0)
            return -1;
 
        // Area of the square
        float area = (float)(Math.Pow(1.268, 2) * Math.Pow(a, 2));
        return area;
    }
 
    // Driver code
    public static void Main()
    {
        float a = 6;
        Console.WriteLine(squareArea(a));
    }
}
 
// This code is contributed by  anuj_67..


PHP


Javascript


输出:
57.8817

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