📌  相关文章
📜  容纳两个相同矩形的正方形的最小面积

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

给定两个相同矩形的长度L和宽度B ,任务是找到可以嵌入尺寸为L × B的两个相同矩形的正方形的最小面积。
例子:

方法:

  • 如果矩形的一边小于或等于另一边长度的一半,则正方形的边是矩形的长边。
  • 如果小边长度的两倍大于大边的长度,则正方形的边是矩形小边长度的两倍。

下面是上述方法的实现:

C++
// C++ program for the above problem
#include 
using namespace std;
 
// Function to find the
// area of the square
int areaSquare(int L, int B)
{
    // Larger side of rectangle
    int large = max(L, B);
 
    // Smaller side of the rectangle
    int small = min(L, B);
 
    if (large >= 2 * small)
        return large * large;
    else
        return (2 * small) * (2 * small);
}
 
// Driver code
int main()
{
    int L = 7;
    int B = 4;
    cout << areaSquare(L, B);
    return 0;
}


Java
// Java implementation of the above approach
import java.io.*;
 
class GFG{
 
// Function to find the
// area of the square
static int areaSquare(int L, int B)
{
     
    // Larger side of rectangle
    int large = Math.max(L, B);
 
    // Smaller side of the rectangle
    int small = Math.min(L, B);
 
    if (large >= 2 * small)
    {
        return large * large;
    }
    else
    {
        return (2 * small) * (2 * small);
    }
}
 
// Driver code
public static void main(String[] args)
{
    int L = 7;
    int B = 4;
     
    System.out.println(areaSquare(L, B));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the above problem
 
# Function to find the
# area of the square
def areaSquare(L, B):
 
    # Larger side of rectangle
    large = max(L, B)
 
    # Smaller side of the rectangle
    small = min(L, B)
 
    if(large >= 2 * small):
        return large * large
    else:
        return (2 * small) * (2 * small)
         
# Driver code
if __name__ == '__main__':
 
    L = 7
    B = 4
     
    print(areaSquare(L, B))
 
# This code is contributed by Shivam Singh


C#
// C# program for the above problem
using System;
 
class GFG{
 
// Function to find the
// area of the square
public static int areaSquare(int L, int B)
{
     
    // Larger side of rectangle
    int large = Math.Max(L, B);
 
    // Smaller side of the rectangle
    int small = Math.Min(L, B);
 
    if (large >= 2 * small)
    {
        return large * large;
    }
    else
    {
        return (2 * small) * (2 * small);
    }
}
 
// Driver code
public static void Main()
{
    int L = 7;
    int B = 4;
     
    Console.Write(areaSquare(L, B));
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
64

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

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