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

📅  最后修改于: 2021-10-23 08:34:14             🧑  作者: 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
?>


Javascript


输出 :

6 

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