在此给出了边长为a的等边三角形,该三角形刻有六边形,而六角形又刻有正方形。任务是找到正方形的边长。
例子:
Input: a = 6
Output: 2.538
Input: a = 8
Output: 3.384
方法:
我们知道,刻在等边三角形内的六边形的边长为h = a / 3 。请参考可刻在等边三角形内的最大六角形。
另外,可以在六边形内刻入的正方形的边长为x = 1.268h。请参考可以在六边形内刻入的最大正方形。
因此,内接在六边形内的正方形的边长又接在等边三角形内, x = 0.423a 。
下面是上述方法的实现:
C++
// C++ program to find the side of the largest square
// that can be inscribed within the hexagon which in return
// is incsribed within an equilateral triangle
#include
using namespace std;
// Function to find the side
// of the square
float squareSide(float a)
{
// Side cannot be negative
if (a < 0)
return -1;
// side of the square
float x = 0.423 * a;
return x;
}
// Driver code
int main()
{
float a = 8;
cout << squareSide(a) << endl;
return 0;
}
Java
// Java program to find the side of the
// largest square that can be inscribed
// within the hexagon which in return is
// incsribed within an equilateral triangle
class cfg
{
// Function to find the side
// of the square
static float squareSide(float a)
{
// Side cannot be negative
if (a < 0)
return -1;
// side of the square
float x = (0.423f * a);
return x;
}
// Driver code
public static void main(String[] args)
{
float a = 8;
System.out.println(squareSide(a));
}
}
// This code is contributed by
// Mukul Singh.
Python3
# Python 3 program to find the side of the
# largest square that can be inscribed
# within the hexagon which in return
# is incsribed within an equilateral triangle
# Function to find the side of the square
def squareSide(a):
# Side cannot be negative
if (a < 0):
return -1
# side of the square
x = 0.423 * a
return x
# Driver code
if __name__ == '__main__':
a = 8
print(squareSide(a))
# This code is contributed by
# Sanjit_Prasad
C#
// C# program to find the side of the
// largest square that can be inscribed
// within the hexagon which in return is
// incsribed within an equilateral triangle
using System;
class GFG
{
// Function to find the side
// of the square
static float squareSide(float a)
{
// Side cannot be negative
if (a < 0)
return -1;
// side of the square
float x = (0.423f * a);
return x;
}
// Driver code
public static void Main()
{
float a = 8;
Console.WriteLine(squareSide(a));
}
}
// This code is contributed by
// shs
PHP
Javascript
输出:
3.384