📌  相关文章
📜  分别平行于 X 和 Y 轴的 N 和 M 条直线可能的矩形计数

📅  最后修改于: 2021-10-23 08:10:42             🧑  作者: Mango

给定两个整数NM ,其中N 条直线平行于X 轴M 条直线平行于Y 轴,任务是计算这些直线可以形成的矩形的数量。
例子:

方法:
为了解决上面提到的问题,我们需要观察到一个矩形由4条对边平行且任意两条边夹角为90的直线组成。因此,对于每个矩形,两条边都需要平行于X-轴和其他两侧需要平行于 Y 轴。

  • 选择平行于 X 轴的两条线的方法数 = N C 2 和选择平行于 Y 轴的两条线的方法数 = M C 2 .
  • 所以矩形的总数 = N C 2 * M C 2 = [ N * (N – 1) / 2 ] * [ M * (M – 1) / 2 ]

以下是上述方法的实现:

C++
// C++ Program to count number of
// rectangles formed by N lines
// parallel to X axis M lines
// parallel to Y axis
#include 
using namespace std;
 
// Function to calculate
// number of rectangles
int count_rectangles(int N, int M)
{
    // Total number of ways to
    // select two lines
    // parallel to X axis
    int p_x = (N * (N - 1)) / 2;
 
    // Total number of ways
    // to select two lines
    // parallel to Y axis
    int p_y = (M * (M - 1)) / 2;
 
    // Total number of rectangles
    return p_x * p_y;
}
 
// Driver Program
int main()
{
 
    int N = 3;
 
    int M = 6;
 
    cout << count_rectangles(N, M);
}


Java
// Java Program to count number of
// rectangles formed by N lines
// parallel to X axis M lines
// parallel to Y axis
class GFG{
 
// Function to calculate
// number of rectangles
static int count_rectangles(int N, int M)
{
    // Total number of ways to
    // select two lines
    // parallel to X axis
    int p_x = (N * (N - 1)) / 2;
 
    // Total number of ways
    // to select two lines
    // parallel to Y axis
    int p_y = (M * (M - 1)) / 2;
 
    // Total number of rectangles
    return p_x * p_y;
}
 
// Driver Program
public static void main(String[] args)
{
    int N = 3;
    int M = 6;
 
    System.out.print(count_rectangles(N, M));
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python3 program to count number of rectangles
# formed by N lines parallel to X axis
# and M lines parallel to Y axis
def count_rectangles(N, M):
 
    # Total number of ways to select
    # two lines parallel to X axis
    p_x = (N * (N - 1)) // 2
 
    # Total number of ways to select
    # two lines parallel to Y axis
    p_y = (M * (M - 1)) // 2
 
    # Total number of rectangles
    return p_x * p_y
 
# Driver code
N = 3
M = 6
 
print(count_rectangles(N, M))
 
# This code is contributed by himanshu77


C#
// C# Program to count number of
// rectangles formed by N lines
// parallel to X axis M lines
// parallel to Y axis
using System;
class GFG{
 
// Function to calculate
// number of rectangles
static int count_rectangles(int N, int M)
{
    // Total number of ways to
    // select two lines
    // parallel to X axis
    int p_x = (N * (N - 1)) / 2;
 
    // Total number of ways
    // to select two lines
    // parallel to Y axis
    int p_y = (M * (M - 1)) / 2;
 
    // Total number of rectangles
    return p_x * p_y;
}
 
// Driver Program
public static void Main()
{
    int N = 3;
    int M = 6;
 
    Console.Write(count_rectangles(N, M));
}
}
 
// This code is contributed by Code_mech


Javascript


输出:
45

时间复杂度: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程