📜  通过N个平面的相交形成的最大线相交数

📅  最后修改于: 2021-04-27 23:49:30             🧑  作者: Mango

给定N架飞机。任务是找到可以通过N个平面的相交形成的最大线相交数。
例子:

方法:
假设有N个平面,使得没有3个平面在一条相交线上相交,并且没有2个平面彼此平行。在保留上述两个条件的同时,应该可以在此空间中添加第N + 1个平面。在这种情况下,该平面将以“ N”条不同的线与N个平面的每一个相交。
因此,第“ N + 1”个平面最多可以将“ N”个新线添加到相交线的总数中。同样,第N个平面最多可以添加“ N-1”个相交线。因此,很容易看到,对于“ N”个平面,相交的最大线数可能是:

(N-1) + (N-2) +...+ 1 = N*(N-1)/2

下面是上述方法的实现:

C++
// C++ implementation of the above pproach
#include 
using namespace std;
 
// Function to count maximum number of
// intersections possible
int countIntersections(int n)
{
    return n * (n - 1) / 2;
}
 
// Driver Code
int main()
{
    int n = 3;
 
    cout << countIntersections(n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
    // Function to count maximum number of
    // intersections possible
    static int countIntersections(int n)
    {
        return n * (n - 1) / 2;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int n = 3;
     
        System.out.println(countIntersections(n));
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the above pproach
 
# Function to count maximum number of
# intersections possible
def countIntersections(n):
    return n * (n - 1) // 2
 
# Driver Code
n = 3
 
print(countIntersections(n))
 
# This code is contributed by mohit kumar


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
    // Function to count maximum number of
    // intersections possible
    static int countIntersections(int n)
    {
        return n * (n - 1) / 2;
    }
     
    // Driver Code
    public static void Main (String[] args)
    {
        int n = 3;
     
        Console.WriteLine(countIntersections(n));
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:
3

时间复杂度: O(1)