📌  相关文章
📜  对给定数组中的相似矩形对进行计数

📅  最后修改于: 2021-05-17 22:18:45             🧑  作者: Mango

给定大小为N (1≤N≤10 3 )的2D数组A [] [2] ,其中A [i] [0]A [i] [1]分别表示矩形i的长度和宽度。

任务是计算几乎相似的一对矩形。

例子:

方法:按照步骤解决问题

  • 遍历数组。
  • 对于每个( i )对,通过检查条件A [i] [0] / A [i] [1] = A [j] [0] / A [ j] [1]是否满足。
  • 如果发现为真,则增加计数。
  • 最后,打印获得的计数。

下面是上述方法的实现:

C++
// C++ Program for the above approach
 
#include 
using namespace std;
 
// Function to calculate the count
// of similar rectangles
int getCount(int rows,
             int columns, int A[][2])
{
    int res = 0;
 
    for (int i = 0; i < rows; i++) {
        for (int j = i + 1; j < rows; j++) {
 
            if (A[i][0] * 1LL * A[j][1]
                == A[i][1] * 1LL * A[j][0]) {
 
                res++;
            }
        }
    }
    return res;
}
 
// Driver Code
int main()
{
    // Input
    int A[][2]
        = { { 4, 8 }, { 10, 20 }, { 15, 30 }, { 3, 6 } };
    int columns = 2;
    int rows = sizeof(A) / sizeof(A[0]);
 
    cout << getCount(rows, columns, A);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
  
// Function to calculate the count
// of similar rectangles
static int getCount(int rows, int columns,
                    int[][] A)
{
    int res = 0;  
    for(int i = 0; i < rows; i++)
    {
        for(int j = i + 1; j < rows; j++)
        {
            if (A[i][0] * A[j][1] ==
                A[i][1] * A[j][0])
            {
                res++;
            }
        }
    }
    return res;
}
  
// Driver Code
public static void main(String[] args)
{
   
    // Input
    int[][] A = { { 4, 8 }, { 10, 20 },
                 { 15, 30 }, { 3, 6 } };
    int columns = 2;
    int rows = 4;
   
    System.out.print(getCount(rows, columns, A));
}
}
 
// This code is contributed by code_hunt.


Python3
# Python3 program for the above approach
 
# Function to calculate the count
# of similar rectangles
def getCount(rows, columns, A):
     
    res = 0
 
    for i in range(rows):
        for j in range(i + 1, rows, 1):
            if (A[i][0] * A[j][1] ==
                A[i][1] * A[j][0]):
                res += 1
                 
    return res
 
# Driver Code
if __name__ == '__main__':
     
    # Input
    A =  [ [ 4, 8 ], [ 10, 20 ],
           [ 15, 30 ], [ 3, 6 ] ]
    columns = 2
    rows =  len(A)
 
    print(getCount(rows, columns, A))
 
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to calculate the count
// of similar rectangles
static int getCount(int rows, int columns,
                    int[,] A)
{
    int res = 0;
   
    for(int i = 0; i < rows; i++)
    {
        for(int j = i + 1; j < rows; j++)
        {
            if (A[i, 0] * A[j, 1] ==
                A[i, 1] * A[j, 0])
            {
                res++;
            }
        }
    }
    return res;
}
 
// Driver code
static void Main()
{
     
    // Input
    int[,] A = { { 4, 8 }, { 10, 20 },
                 { 15, 30 }, { 3, 6 } };
    int columns = 2;
    int rows = 4;
   
    Console.Write(getCount(rows, columns, A));
}
}
 
// This code is contributed by divyesh072019


Javascript


输出:
6

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