📌  相关文章
📜  通过将n边多边形的顶点与两个公共边和非公共边相连而形成的三角形数量

📅  最后修改于: 2021-05-06 19:17:58             🧑  作者: Mango

给定N边多边形,我们需要找到通过将给定多边形的顶点恰好有两个边是公共的且没有边是公共的连接在一起而形成的三角形总数。
例子:

方法 :

  • 为了使三角形的两个边与多边形共同,我们将采用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)