📌  相关文章
📜  使用正方形上的点形成三角形

📅  最后修改于: 2021-06-25 13:18:06             🧑  作者: Mango

给定一个正方形,在正方形的两边各有N个点,并且这些点均不与正方形的边角重合。任务是使用这4个* N点(正方形两边的N个点)作为三角形的顶点,计算可以形成的三角形总数。
例子:

方法:4 * N点中选择3个点的方式为(4 * N) C 3 。但是,其中一些不形成三角形。当所有三个选定点都位于正方形的同一侧时,会发生这种情况。这些三面体的数目对于每一侧为N C 3 ,即总计为4 * N C 3 。因此,所需的三角形数将为( (4 * N) C 3 )–(4 * N C 3 )
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count
// of possible triangles
int noOfTriangles(int n)
{
    int y = 4 * n;
    return ((y * (y - 2) * (y - 1))
            - (4 * n * (n - 2) * (n - 1)))
           / 6;
}
 
// Driver code
int main()
{
    int n = 1;
 
    cout << noOfTriangles(n);
 
    return 0;
}


Java
// Java implementation of the above approach
class GFG
{
         
    // Function to return the count
    // of possible triangles
    static int noOfTriangles(int n)
    {
        int y = 4 * n;
        return ((y * (y - 2) * (y - 1)) -
                (4 * n * (n - 2) * (n - 1))) / 6;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 1;
     
        System.out.println(noOfTriangles(n));
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach
  
# Function to return the count
# of possible triangles
def noOfTriangles(n):
    y = 4 * n
    return ((y * (y - 2) * (y - 1)) -
            (4 * n * (n - 2) * (n - 1)))// 6
 
# Driver code
n = 1
 
print(noOfTriangles(n))
 
# This code is contributed by Mohit Kumar


C#
// C# implementation of the above approach
using System;
     
class GFG
{
         
    // Function to return the count
    // of possible triangles
    static int noOfTriangles(int n)
    {
        int y = 4 * n;
        return ((y * (y - 2) * (y - 1)) -
                (4 * n * (n - 2) * (n - 1))) / 6;
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        int n = 1;
     
        Console.WriteLine(noOfTriangles(n));
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
4