📌  相关文章
📜  计算给定数组中可能的相似矩形对

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

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

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

例子:

方法一(简单比较法)

方法:按照步骤解决问题

  • 遍历数组。
  • 对于每一对( i < j ),通过检查条件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


Java
import java.util.*;
 
class GFG {
 
      // Get the count of all pairs of similar rectangles
    public static int getCount(int rows, int columns,
                                int[][] sides)
    {
          // Initialize the result value and
          // map to store the ratio to the rectangles
        int res = 0;
        Map ratio
            = new HashMap();
 
          // Calculate the rectangular ratio and save them
          for (int i = 0; i < rows; i++) {
            double rectRatio = (double) sides[i][0] /
                  sides[i][1];
              if (ratio.get(rectRatio) == null) {
                  ratio.put(rectRatio, 0);
            }
              ratio.put(rectRatio, ratio.get(rectRatio) + 1);
        }
 
          // Calculate pairs of similar rectangles from
          // its common ratio
        for (double key : ratio.keySet()) {
            int val = ratio.get(key);
            if (val > 1) {
                res += (val * (val - 1)) / 2;
            }
        }
 
        return res;
    }
  
      public static void main(String[] args) {
          int[][] A = {{4, 8}, {10, 20}, {15, 30}, {3, 6}};
          int columns = 2;
          int rows = 4;
           
          System.out.println(getCount(rows, columns, A));
    }
}
 
// This code is contributed by nathnet


输出
6

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

方法二(使用HashMap)

我们可以将所有计算出的比率存储在 HashMap 中,而不是直接将一个矩形的比率与另一个矩形的比率进行比较。由于 HashMap 的访问成本为 O(1),因此可以快速查找具有相同比率的其他矩形并将它们存储在一起。相似矩形的对数可以使用\frac{N \times (N – 1)}{2} 从具有相同比率的矩形的数量中推导出来。可以使用上述方程找到相似矩形对数的原因是对数增加了N-1   每次添加一个具有相同比例的矩形。

Java

import java.util.*;
 
class GFG {
 
      // Get the count of all pairs of similar rectangles
    public static int getCount(int rows, int columns,
                                int[][] sides)
    {
          // Initialize the result value and
          // map to store the ratio to the rectangles
        int res = 0;
        Map ratio
            = new HashMap();
 
          // Calculate the rectangular ratio and save them
          for (int i = 0; i < rows; i++) {
            double rectRatio = (double) sides[i][0] /
                  sides[i][1];
              if (ratio.get(rectRatio) == null) {
                  ratio.put(rectRatio, 0);
            }
              ratio.put(rectRatio, ratio.get(rectRatio) + 1);
        }
 
          // Calculate pairs of similar rectangles from
          // its common ratio
        for (double key : ratio.keySet()) {
            int val = ratio.get(key);
            if (val > 1) {
                res += (val * (val - 1)) / 2;
            }
        }
 
        return res;
    }
  
      public static void main(String[] args) {
          int[][] A = {{4, 8}, {10, 20}, {15, 30}, {3, 6}};
          int columns = 2;
          int rows = 4;
           
          System.out.println(getCount(rows, columns, A));
    }
}
 
// This code is contributed by nathnet
输出
6

时间复杂度 O(N)

辅助空间 O(N)