给定N边多边形,我们需要找到通过将给定多边形的顶点恰好有两个边是公共的且没有边是公共的连接在一起而形成的三角形总数。
例子:
Input : N = 6
Output : 6 2
The image below is of a triangle forming inside a Hexagon by joining vertices as shown above.
The triangle formed has two sides (AB and BC) common with that of a polygon. Similarly BC and
CD can make one triangle. With this, we can say that there will be a total of 6 triangles possible
having two sides common with that of a polygon. The second image of a hexagon,
a triangle is formed with no side common with that of a polygon.
There will be just 2 triangles possible, BFD and ACE.
Number of triangles formed are 6 and 2 with two side common and with no side common respectively.
Input : N = 7
Output : 7 7
方法 :
- 为了使三角形的两个边与多边形共同,我们将采用n边多边形的任意一侧,采用所选侧的一个顶点,并连接与另一个顶点的顶点相邻的边。
- 遍历每个顶点并邻接与另一个顶点的顶点相邻的边,将有N个三角形,它们的两个侧面共同。
- 现在,要计算没有边公共的三角形的数量,请从多边形中可能存在的三角形总数中减去边公共的三角形总数和边是两侧的三角形总数。
- 无公共边的三角形=总三角形( n C 3 )–一侧有公共三角形(n *(n – 4)–两侧有公共三角形(n)。
- 因此,与多边形没有共同边的三角形数量等于n *(n-4)*(n-5)/ 6。
注意:要计算一侧与多边形相同的三角形的数量,请单击此处
下面是上述方法的实现:
C++
// C++ program to implement
// the above problem
#include
using namespace std;
// Function to find the number of triangles
void findTriangles(int n)
{
int num = n;
// print the number of triangles
// having two side common
cout << num << " ";
// print the number of triangles
// having no side common
cout << num * (num - 4) * (num - 5) / 6;
}
// Driver code
int main()
{
// initialize the number
// of sides of a polygon
int n;
n = 6;
findTriangles(n);
return 0;
}
Java
// Java program to implement
// the above problem
import java.io.*;
class GFG
{
// Function to find the number of triangles
static void findTriangles(int n)
{
int num = n;
// print the number of triangles
// having two side common
System.out.print( num + " ");
// print the number of triangles
// having no side common
System.out.print( num * (num - 4) * (num - 5) / 6);
}
// Driver code
public static void main (String[] args)
{
// initialize the number
// of sides of a polygon
int n;
n = 6;
findTriangles(n);
}
}
// This code is contributed by anuj_67..
Python3
# Python3 program to implement
# the above problem
# Function to find the number of triangles
def findTriangles(n):
num = n
# print the number of triangles
# having two side common
print(num, end = " ")
# print the number of triangles
# having no side common
print(num * (num - 4) * (num - 5) // 6)
# Driver code
# initialize the number
# of sides of a polygon
n = 6;
findTriangles(n)
# This code is contributed by Mohit Kumar
C#
// C# program to implement
// the above problem
using System;
class GFG
{
// Function to find the number of triangles
static void findTriangles(int n)
{
int num = n;
// print the number of triangles
// having two side common
Console.Write( num + " ");
// print the number of triangles
// having no side common
Console.WriteLine( num * (num - 4) * (num - 5) / 6);
}
// Driver code
public static void Main ()
{
// initialize the number
// of sides of a polygon
int n;
n = 6;
findTriangles(n);
}
}
// This code is contributed by anuj_67..
Javascript
6 2
时间复杂度: O(1)