给定 N 个平面。任务是找到通过N个平面的交点可以形成的最大线交点数。
例子:
Input: N = 3
Output: 3
Input: N = 5
Output: 10
方法:
设 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)