📌  相关文章
📜  使用N个单位正方形形成的唯一矩形的数量

📅  最后修改于: 2021-04-25 00:39:47             🧑  作者: Mango

给您N个单位正方形(边长为1个单位的正方形),并要求您使用这些正方形制作矩形。您必须计算出无法旋转的唯一矩形的数量。旋转唯一是什么意思?好吧,如果一个矩形无法旋转以与另一个矩形等效,则两个矩形在旋转方面是唯一的。

示例– 4×2矩形可以顺时针旋转90度,使其与2×4矩形完全相同,因此它们在旋转方面不是唯一的。

例子 :

Input : N = 4
Output : 5
We can make following five rectangles 
1 x 1, 1 x 2, 2 x 2, 1 x 3 and 1 x 4

Input : N = 5
Output : 6

Input : 6
Output : 8

那么我们如何解决这个问题呢?
每个矩形都由其长度和高度唯一地确定。
长度为l且高度为h的矩形,则l * h <= n等效于长度为h且高度为l的矩形,前提是l不等于h。如果我们在这些对中可以有某种“排序”,则可以避免将(l,h)和(h,l)计为不同的矩形。定义这种顺序的一种方法是:
假设长度<=高度,并对所有此类对进行计数,以使长度*高度<= n。

我们有,长度<=高度
或者,长度*长度<=长度*高度
或者,长度*长度<= n
或者,长度<= sqrt(n)

C++
// C++ program to count rotationally equivalent
// rectangles with n unit squares
#include
using namespace std;
  
int countRect(int n)
{
    int ans = 0;
    for (int length = 1; length <= sqrt(n); ++length)
    for (int height = length; height*length <= n; ++height)
            // height >= length is maintained
        ans++;
    return ans;
}
  
// Driver code
int main() {
    int n = 5;
    printf("%d", countRect(n));
    return 0;
}


Java
// Java program to count rotationally equivalent
// rectangles with n unit squares
class GFG {
      
    static int countRect(int n)
    {
        int ans = 0;
          
        for (int length = 1; length <= Math.sqrt(n);
                                           ++length)
            for (int height = length; height*length <= n;
                                                ++height)
                // height >= length is maintained
                ans++;
              
        return ans;
    }
      
    //driver code
    public static void main (String[] args)
    {
        int n = 5;
          
        System.out.print(countRect(n));
    }
}
  
// This code is contributed by Anant Agarwal.


Python3
# Python3 program to count rotationally 
# equivalent rectangles with n unit squares
import math
  
def countRect(n):
  
    ans = 0
    for length in range(1, int(math.sqrt(n)) + 1):
        height = length
        while(height * length <= n):
              
            # height >= length is maintained
            ans += 1
            height += 1
    return ans
  
# Driver code
n = 5
print(countRect(n))
  
# This code is contributed by Anant Agarwal.


C#
// C# program to count rotationally 
// equivalent rectangles with n unit
// squares
using System;
  
class GFG {
      
    static int countRect(int n)
    {
        int ans = 0;
        for (int length = 1; 
            length <= Math.Sqrt(n); ++length)
              
            for (int height = length; 
                     height*length <= n; ++height)
                ans++;
                  
        return ans;
    }
      
    //driver code
    public static void Main()
    {
          
        int n = 5;
          
        Console.Write(countRect(n));
    }
}
  
//This code is contributed by Anant Agarwal.


PHP
= length is maintained
        $ans++;
    return $ans;
}
  
// Driver code
$n = 5;
echo countRect($n);
  
// This code is contributed by @ajit
?>


输出 :

6