给定一个由六边形的6个边的长度组成的数组S [] ,任务是计算可以从给定的六边形中得出的单位长度的等边三角形的数量。
例子:
Input: S = {1, 1, 1, 1, 1, 1}
Output: 6
Explanation:
Input: S = {2, 2, 1, 3, 1, 2}
Output: 19
Explanation:
方法:需要解决以下问题以解决给定的问题:
- 考虑边长为“ X”的等边三角形。通过绘制平行于其侧面的线,它已被划分为每个单位长度的较小三角形。
- 下面是三个这样的等边三角形的图像:
- 在以上三个示例中的每个示例中,可能的单位长度等边三角形的计数为:
- X = 2: 4 equilateral triangles of 1 unit length side.
- X = 3: 9 equilateral triangles of 1 unit length side.
- X = 5: 25 equilateral triangles of 1 unit length side.
- 通过观察,很明显,对于边长为X的等边三角形, X 2个单位长度的等边三角形是可能的。
- 将此观察结果扩展到六边形,在等边三角形内刻上六边形,如下所示:
- 可以观察到,通过从较大的三角形中删除一定数量的微型三角形,可以找到具有给定尺寸的六边形。
对于具有六个边S 1 ,S 2 ,S 3 ,S 4 ,S 5 ,S 6的六边形,可以将计算单位长度的三角形数量的公式推广为:
Number of triangles that can be formed = ( S1 + S2 + S3 )2 – S12 – S32 – S52
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to calculate the
// the number of Triangles possible
int calculateTriangles(int sides[])
{
double count = pow(sides[0] + sides[1] +
sides[2], 2);
count -= pow(sides[0], 2);
count -= pow(sides[2], 2);
count -= pow(sides[4], 2);
return (int)(count);
}
// Driver Code
int main()
{
// Regular Hexagon
int sides[] = { 1, 1, 1, 1, 1, 1 };
cout << (calculateTriangles(sides)) << endl;
// Irregular Hexagon
int sides1[] = { 2, 2, 1, 3, 1, 2 };
cout << (calculateTriangles(sides1)) << endl;
return 0;
}
// This code is contributed by 29AjayKumar
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to calculate the
// the number of Triangles possible
static int calculateTriangles(int sides[])
{
double count = Math.pow(sides[0] + sides[1] +
sides[2], 2);
count -= Math.pow(sides[0], 2);
count -= Math.pow(sides[2], 2);
count -= Math.pow(sides[4], 2);
return (int)(count);
}
// Driver Code
public static void main(String[] args)
{
// Regular Hexagon
int sides[] = { 1, 1, 1, 1, 1, 1 };
System.out.print((calculateTriangles(sides)) + "\n");
// Irregular Hexagon
int sides1[] = { 2, 2, 1, 3, 1, 2 };
System.out.print((calculateTriangles(sides1)) + "\n");
}
}
// This code is contributed by amal kumar choubey
Python3
# Python3 Program to implement
# the above approach
# Function to calculate the
# the number of Triangles possible
def calculateTriangles(sides):
count = pow( sides[0] + sides[1] + sides[2], 2)
count -= pow( sides[0], 2)
count -= pow( sides[2], 2)
count -= pow( sides[4], 2)
return int(count)
# Driver Code
# Regular Hexagon
sides = [1, 1, 1, 1, 1, 1]
print(calculateTriangles(sides))
# Irregular Hexagon
sides = [2, 2, 1, 3, 1, 2]
print(calculateTriangles(sides))
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to calculate the
// the number of Triangles possible
static int calculateTriangles(int []sides)
{
double count = Math.Pow(sides[0] + sides[1] +
sides[2], 2);
count -= Math.Pow(sides[0], 2);
count -= Math.Pow(sides[2], 2);
count -= Math.Pow(sides[4], 2);
return (int)(count);
}
// Driver Code
public static void Main(String[] args)
{
// Regular Hexagon
int []sides = { 1, 1, 1, 1, 1, 1 };
Console.Write((calculateTriangles(sides)) + "\n");
// Irregular Hexagon
int []sides1 = { 2, 2, 1, 3, 1, 2 };
Console.Write((calculateTriangles(sides1)) + "\n");
}
}
// This code is contributed by amal kumar choubey
输出:
6
19
时间复杂度: O(1)
辅助空间: O(1)