📜  最大交点n条线

📅  最后修改于: 2021-10-23 09:00:45             🧑  作者: Mango

给你 n 条直线。您必须找到与这 n 条线相交的最大数量的点。
例子:

Input : n = 4 
Output : 6

Input : n = 2
Output :1

图片

方法 :
由于我们有 n 条线,我们必须使用这 n 条线找到最大交点。所以这可以使用组合来完成。这个问题可以被认为是在n行中选择任意两条线的多种方法。由于每条线都与选定的其他线相交。
所以,总点数 = nC2
下面是上述方法的实现:

C++
// CPP program to find maximum intersecting
// points
#include 
using namespace std;
#define ll long int
 
 
// nC2 = (n)*(n-1)/2;
ll countMaxIntersect(ll n)
{
   return (n) * (n - 1) / 2;
}
 
// Driver code
int main()
{
    // n is number of line
    ll n = 8;
    cout << countMaxIntersect(n) << endl;
    return 0;
}


Java
// Java program to find maximum intersecting
// points
 
public class GFG {
     
    // nC2 = (n)*(n-1)/2;
    static long countMaxIntersect(long n)
    {
       return (n) * (n - 1) / 2;
    }
 
     
    // Driver code
    public static void main(String args[])
    {
        // n is number of line
        long n = 8;
        System.out.println(countMaxIntersect(n));
 
 
    }
    // This code is contributed by ANKITRAI1
}


Python3
# Python3 program to find maximum
# intersecting points
 
#nC2 = (n)*(n-1)/2
def countMaxIntersect(n):
    return int(n*(n - 1)/2)
 
#Driver code
if __name__=='__main__':
     
# n is number of line
    n = 8
    print(countMaxIntersect(n))
 
# this code is contributed by
# Shashank_Sharma


C#
// C# program to find maximum intersecting
// points
using System;
 
class GFG
{
     
    // nC2 = (n)*(n-1)/2;
    public static long countMaxIntersect(long n)
    {
    return (n) * (n - 1) / 2;
    }
 
     
    // Driver code
    public static void Main()
    {
        // n is number of line
        long n = 8;
        Console.WriteLine(countMaxIntersect(n));
    }
}
// This code is contributed by Soumik


PHP


Javascript


输出:
28