📌  相关文章
📜  由三条线上的一组点形成的三角形数量

📅  最后修改于: 2021-04-28 00:00:47             🧑  作者: Mango

给定三个整数m,n和k ,它们分别存储在线l1,l2和l3上不相交的点的数量。任务是找到可以从这些点集合中形成的三角形数量。
例子:

Input: m = 3, n =  4, k = 5 
Output: 205

Input: m = 2, n =  2, k = 1 
Output: 10

方法:

  • 总点数为(m + n + k),必须给出(m + n + k)_{C_3}  三角形数量。
  • 但是’l1’上的’m’点给出m_{C_3}  不能形成三角形的组合。
  • 相似地, n_{C_3}  k_{C_3}  不能形成三角形数量。
  • 因此,所需的三角形数= (m + n + k)_{C_3} - m_{C_3} - n_{C_3} - k_{C_3}

下面是上述方法的实现:

C++
// CPP program to find the possible number
// of triangles that can be formed from
// set of points on three lines
#include 
using namespace std;
 
// Returns factorial of a number
int factorial(int n)
{
    int fact = 1;
    for (int i = 2; i <= n; i++)
        fact = fact * i;
    return fact;
}
 
// calculate c(n, r)
int ncr(int n, int r)
{
 
    return factorial(n)
           / (factorial(r) * factorial(n - r));
}
 
// Driver code
int main()
{
    int m = 3, n = 4, k = 5;
    int totalTriangles
        = ncr(m + n + k, 3)
          - ncr(m, 3) - ncr(n, 3) - ncr(k, 3);
    cout << totalTriangles << endl;
}


Java
//Java  program to find the possible number
// of triangles that can be formed from
// set of points on three lines
 
import java.io.*;
 
class GFG {
     
     
// Returns factorial of a number
static int factorial(int n)
{
    int fact = 1;
    for (int i = 2; i <= n; i++)
        fact = fact * i;
    return fact;
}
 
// calculate c(n, r)
static int ncr(int n, int r)
{
 
    return factorial(n)
        / (factorial(r) * factorial(n - r));
}
 
// Driver code
     
    public static void main (String[] args) {
 
        int m = 3, n = 4, k = 5;
        int totalTriangles = ncr(m + n + k, 3) -
           ncr(m, 3) - ncr(n, 3) - ncr(k, 3);
        System.out.println (totalTriangles);
         
         
    }
}


Python 3
# Python 3 program to find the
# possible number of triangles
# that can be formed from set of
# points on three lines
 
 
# Returns factorial of a number
def factorial(n):
    fact = 1
    for i in range(2, n + 1):
        fact = fact * i
    return fact
 
# calculate c(n, r)
def ncr(n, r):
 
    return (factorial(n) // (factorial(r) *
                             factorial(n - r)))
 
# Driver code
if __name__ == "__main__":
    m = 3
    n = 4
    k = 5
    totalTriangles = (ncr(m + n + k, 3) -
                      ncr(m, 3) - ncr(n, 3) -
                      ncr(k, 3))
    print(totalTriangles)
 
# This code is contributed
# by ChitraNayal


C#
// C# program to find the possible number
// of triangles that can be formed from
// set of points on three lines
using System;
 
class GFG
{
     
// Returns factorial of a number
static int factorial(int n)
{
    int fact = 1;
    for (int i = 2; i <= n; i++)
        fact = fact * i;
    return fact;
}
 
// calculate c(n, r)
static int ncr(int n, int r)
{
 
    return factorial(n) / (factorial(r) *
                           factorial(n - r));
}
 
// Driver code
public static void Main ()
{
    int m = 3, n = 4, k = 5;
     
    int totalTriangles = ncr(m + n + k, 3) -
                         ncr(m, 3) - ncr(n, 3) -
                         ncr(k, 3);
                          
    Console.WriteLine (totalTriangles);
}
}
 
// This code is contributed
// by anuj_67..


PHP


Javascript


输出:
205