这里给出一个边长为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
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。