📌  相关文章
📜  可以从一张纸上剪下的最大给定大小的矩形

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

给定一张纸的长度L和宽度B ,任务是找到可以从这张纸上剪下的具有给定长度l和宽度b的矩形的最大数量。
例子:

方法:

  • 尝试水平切割矩形,即矩形的长度与纸张的长度对齐,矩形的宽度与纸张的宽度对齐,并在水平方向上存储可能的矩形数量。
  • 对垂直对齐重复相同的操作,即当矩形的长度与纸张的宽度对齐并且矩形的宽度与纸张的长度对齐时,并将结果存储在vertical 中。打印max(horizontal, vertical)作为结果。

下面是上述方法的实现:

C++
// CPP implementation of the approach
#include
using namespace std;
 
// Function to return the maximum rectangles possible
int maxRectangles(int L, int B, int l, int b)
{
    int horizontal = 0, vertical = 0;
 
    // Cut rectangles horizontally if possible
    if (l <= L && b <= B)
    {
 
        // One rectangle is a single cell
        int columns = B / b;
        int rows = L / l;
 
        // Total rectangles = total cells
        horizontal = rows * columns;
    }
 
    // Cut rectangles vertically if possible
    if (l <= B && b <= L)
    {
        int columns = L / b;
        int rows = B / l;
 
        vertical = rows * columns;
    }
 
    // Return the maximum possible rectangles
    return max(horizontal, vertical);
}
 
// Driver code
int main()
{
    int L = 10, B = 7, l = 4, b = 3;
    cout << (maxRectangles(L, B, l, b)) << endl;
}
 
// This code is contributed by
// Sanjit_Prasad


Java
// Java implementation of the approach
class GFG {
 
    // Function to return the maximum rectangles possible
    static int maxRectangles(int L, int B, int l, int b)
    {
        int horizontal = 0, vertical = 0;
 
        // Cut rectangles horizontally if possible
        if (l <= L && b <= B) {
 
            // One rectangle is a single cell
            int columns = B / b;
            int rows = L / l;
 
            // Total rectangles = total cells
            horizontal = rows * columns;
        }
 
        // Cut rectangles vertically if possible
        if (l <= B && b <= L) {
            int columns = L / b;
            int rows = B / l;
 
            vertical = rows * columns;
        }
 
        // Return the maximum possible rectangles
        return Math.max(horizontal, vertical);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int L = 10, B = 7, l = 4, b = 3;
        System.out.print(maxRectangles(L, B, l, b));
    }
}


Python3
# Python3 implementation of the approach
 
# Function to return the maximum
# rectangles possible
def maxRectangles(L, B, l, b):
 
    horizontal, vertical = 0, 0
 
    # Cut rectangles horizontally if possible
    if l <= L and b <= B:
 
        # One rectangle is a single cell
        columns = B // b
        rows = L // l
 
        # Total rectangles = total cells
        horizontal = rows * columns
 
    # Cut rectangles vertically if possible
    if l <= B and b <= L:
     
        columns = L // b
        rows = B // l
 
        vertical = rows * columns
 
    # Return the maximum possible rectangles
    return max(horizontal, vertical)
 
# Driver code
if __name__ == "__main__":
 
    L, B, l, b = 10, 7, 4, 3
    print(maxRectangles(L, B, l, b))
 
# This code is contributed by Rituraj Jain


C#
// C# implementation of the above approach
using System;
 
class GFG
{
 
    // Function to return the
    // maximum rectangles possible
    static int maxRectangles(int L, int B,
                                int l, int b)
    {
        int horizontal = 0, vertical = 0;
 
        // Cut rectangles horizontally if possible
        if (l <= L && b <= B)
        {
 
            // One rectangle is a single cell
            int columns = B / b;
            int rows = L / l;
 
            // Total rectangles = total cells
            horizontal = rows * columns;
        }
 
        // Cut rectangles vertically if possible
        if (l <= B && b <= L)
        {
            int columns = L / b;
            int rows = B / l;
            vertical = rows * columns;
        }
 
        // Return the maximum possible rectangles
        return Math.Max(horizontal, vertical);
    }
 
    // Driver code
    public static void Main()
    {
        int L = 10, B = 7, l = 4, b = 3;
        Console.WriteLine(maxRectangles(L, B, l, b));
    }
}
 
// This code is contributed by Ryuga


PHP


Javascript


输出:
4

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