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

📅  最后修改于: 2021-04-29 14:12:10             🧑  作者: Mango

给定一张纸的长度L和宽度B ,任务是找到可以从这张纸切出的给定长度L和宽度b的最大矩形数。

例子:

方法:

  • 尝试水平切割矩形,即矩形的长度与图纸的长度对齐,矩形的宽度与图纸的宽度对齐,然后将水平矩形的数量存储在水平
  • 对垂直对齐重复相同的操作,即,当矩形的长度与图纸的宽度对齐且矩形的宽度与图纸的长度对齐时,将结果存储在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


输出:
4