📜  X圆和Y直线之间可能的最大交点

📅  最后修改于: 2021-04-21 22:36:21             🧑  作者: Mango

给定两个整数XY ,任务是找到X圆和Y直线之间可能的最大相交点数。

例子:

方法:
可以观察到有三种类型的交叉点:

  1. 从X个圆中选择一对点的方式有\binom{X}{2}  。每个这样的对最多相交两个点。
  2. 从Y线中选择一对点的方法是\binom{Y}{2}  。每个这样的对最多相交一个点。
  3. 从X圆和Y线中选择一个圆和一条线的方法是X*Y  。每个这样的对最多相交两个点。

因此,求X圆与Y直线的最大交点数的公式为:
\frac{Y*(Y - 1)}{2} + X*(2*Y + X - 1)

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
int maxPointOfIntersection(int x, int y)
{
    int k = y * (y - 1) / 2;
    k = k + x * (2 * y + x - 1);
    return k;
}
 
// Driver code
int main()
{
     
    // Number of circles
    int x = 3;
     
    // Number of straight lines
    int y = 4;
 
    // Function Call
    cout << (maxPointOfIntersection(x, y));
}
 
// This code is contributed by Ritik Bansal


Java
// Java program to implement
// the above approach
class GFG{
     
static int maxPointOfIntersection(int x, int y)
{
    int k = y * (y - 1) / 2;
    k = k + x * (2 * y + x - 1);
    return k;
}
 
// Driver code
public static void main(String[] args)
{
     
    // Number of circles
    int x = 3;
     
    // Number of straight lines
    int y = 4;
 
    // Function Call
    System.out.print(maxPointOfIntersection(x, y));
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 program to implement
# the above approach
def maxPointOfIntersection(x, y):
    k = y * ( y - 1 ) // 2
    k = k + x * ( 2 * y + x - 1 )
    return k
 
# Number of circles
x = 3
# Number of straight lines
y = 4
 
# Function Call
print(maxPointOfIntersection(x, y))


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
     
static int maxPointOfIntersection(int x, int y)
{
    int k = y * (y - 1) / 2;
    k = k + x * (2 * y + x - 1);
    return k;
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Number of circles
    int x = 3;
     
    // Number of straight lines
    int y = 4;
 
    // Function Call
    Console.Write(maxPointOfIntersection(x, y));
}
}
 
// This code is contributed by Princi Singh


Javascript


输出:
36

时间复杂度: O(1)
辅助空间: O(1)