📜  查找正方形的一面,以使其最小的区域适合其中的两个相同的矩形

📅  最后修改于: 2021-04-22 09:00:08             🧑  作者: Mango

给定矩形的高度H和宽度W ,任务是找到最小面积的正方形的边,在该边上两个矩形完全适合。

笔记:

  1. 两个矩形可以并排或靠角彼此接触。
  2. 矩形不能相交。
  3. 矩形也可以接触正方形的侧面,但必须完全在正方形内部。
  4. 矩形也可以旋转

例子 :

方法 :
有三种可能的情况:

  1. 首先检查给定的高度和宽度是否相同,如果相同,则矩形本身就是一个正方形,因此只需附加两个正方形并将侧面加倍,

  1. 然后,如果不满足第一种情况,则检查高度是否大于宽度
    如果更大,则将宽度值加倍,然后将其添加到宽度中,并形成新的宽度,
    • 然后找到‘new_Width’‘height’的差异并将差异存储在新变量“ diff”中
    • 如果‘new_Width’小于‘height’ ,则将 diff’值添加到‘new_Width’ ,然后‘height’‘new_width’相同并返回‘new_Width’ ,即最小正方形的边。
    • 如果‘new_Width’大于‘height’ ,则将 diff’值添加到‘height’ ,然后‘height’‘width’相同,并返回‘height’ ,这是最小值的一面。正方形。
  1. 然后,如果不满足第二种情况,则检查height
    如果较小,则将“ height”值加倍,并将其添加到高度中,然后再设置一个新的高度,
    • 然后找到‘new_Height’‘width’的差异,并将差异存储在新变量“ diff”中
    • 如果‘new_Height’小于‘width’ ,则将 diff’值添加到‘new_Height’ ,然后‘width’‘new_Height’相同并返回‘new_Height’ ,即最小正方形的边。
    • 如果‘new_Height’大于‘width’ ,则将 diff’值添加到‘width’ ,然后‘height’‘width’相同,并返回‘height’ ,这是最小值的一面。正方形。
  1. 结尾

下面是上述方法的实现:

C++
// C++ program of the above approach
 
#include 
using namespace std;
 
int minimalSquareSide(int a, int b)
{
    // if 'a' and 'b' same
    // then double 'a' or 'b'
    // and return (2*a) or (2*b)
    if (a == b) {
        return 2 * a;
    }
 
    // check if a!=b
    if (a != b) {
        // if a > b
        if (a > b) {
 
            // double the smaller value
            // that is 'b' and store
            // it to 'newB'
            int newB = b + b;
 
            // find the difference of
            // 'newB and 'a'
            int diff = abs(newB - a);
 
            // if 'newB' < a
            if (newB < a) {
                // then add the difference of
                // 'newB' and 'a' to the 'b'
                // to make 'b' and 'a' as same
                b = newB + diff;
 
                // return side of the
                // square a or b
                if (a == b)
                    return a;
                return 0;
            }
 
            else {
                // if 'newB' > a then
                // then add the difference
                // of 'newB' and 'a' to
                // the 'a' to make 'a' and
                // 'newB' as same
                a = a + diff;
 
                // return side of the
                // square a or newB
                if (a == newB)
                    return a;
                return 0;
            }
        }
 
        // if a < b
        else {
            // double the smaller value
            // that is 'a' and store
            // it to 'newA'
            int newA = a + a;
 
            // find the difference of
            // 'newA and 'b'
            int diff = abs(newA - b);
 
            // if 'newA' < b
            if (newA < b) {
                // then add the difference
                // of 'newA' and 'b' to
                // the 'a' to make 'a'
                // and 'b' as same
                a = diff + newA;
 
                // return side of the
                // square a or b
                if (a == b)
                    return a;
                return 0;
            }
 
            else {
                // if 'newA' > b then
                // then add the difference
                // of 'newA' and 'b' to
                // the 'b' to make 'b' and
                // 'newA' as same
                b = b + diff;
 
                // return side of the
                // square b or newA
                if (b == newA)
                    return b;
                return 0;
            }
        }
    }
}
 
// Drive Code
int main()
{
    int H, W;
 
    // Size of rectangle
    H = 3, W = 1;
 
    cout << minimalSquareSide(H, W) << endl;
    return 0;
}


Java
// Java program of the above approach
class GFG{
     
public static int minimalSquareSide(int a, int b)
{
     
    // If 'a' and 'b' same
    // then double 'a' or 'b'
    // and return (2*a) or (2*b)
    if (a == b)
    {
        return 2 * a;
    }
     
    // Check if a!=b
    if (a != b)
    {
         
        // If a > b
        if (a > b)
        {
             
            // Double the smaller value
            // that is 'b' and store
            // it to 'newB'
            int newB = b + b;
     
            // Find the difference of
            // 'newB and 'a'
            int diff = Math.abs(newB - a);
     
            // If 'newB' < a
            if (newB < a)
            {
                 
                // Then add the difference of
                // 'newB' and 'a' to the 'b'
                // to make 'b' and 'a' as same
                b = newB + diff;
     
                // Return side of the
                // square a or b
                if (a == b)
                    return a;
                return 0;
            }
            else
            {
                 
                // If 'newB' > a then
                // then add the difference
                // of 'newB' and 'a' to
                // the 'a' to make 'a' and
                // 'newB' as same
                a = a + diff;
     
                // Return side of the
                // square a or newB
                if (a == newB)
                    return a;
                return 0;
            }
        }
     
        // If a < b
        else
        {
             
            // Double the smaller value
            // that is 'a' and store
            // it to 'newA'
            int newA = a + a;
     
            // Find the difference of
            // 'newA and 'b'
            int diff = Math.abs(newA - b);
     
            // If 'newA' < b
            if (newA < b)
            {
                 
                // Then add the difference
                // of 'newA' and 'b' to
                // the 'a' to make 'a'
                // and 'b' as same
                a = diff + newA;
     
                // Return side of the
                // square a or b
                if (a == b)
                    return a;
                return 0;
            }
            else
            {
                 
                // If 'newA' > b then
                // then add the difference
                // of 'newA' and 'b' to
                // the 'b' to make 'b' and
                // 'newA' as same
                b = b + diff;
     
                // Return side of the
                // square b or newA
                if (b == newA)
                    return b;
                return 0;
            }
        }
    }
    return 0;
}
 
// Driver code   
public static void main(String[] args)
{
    int H, W;
 
    // Size of rectangle
    H = 3; W = 1;
     
    System.out.println(minimalSquareSide(H, W));
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 program for the above approach
 
def minimalSquareSide(a, b):
     
    # If 'a' and 'b' same
    # then double 'a' or 'b'
    # and return (2*a) or (2*b)
    if (a == b):
        return 2 * a
 
    # Check if a!=b
    if (a != b):
         
        # If a > b
        if (a > b):
 
            # Double the smaller value
            # that is 'b' and store
            # it to 'newB'
            newB = b + b
 
            # Find the difference of
            # 'newB and 'a'
            diff = abs(newB - a)
 
            # If 'newB' < a
            if (newB < a):
                 
                # Then add the difference of
                # 'newB' and 'a' to the 'b'
                # to make 'b' and 'a' as same
                b = newB + diff
 
                # Return side of the
                # square a or b
                if (a == b):
                    return a
                return 0
 
            else:
                 
                # If 'newB' > a then
                # then add the difference
                # of 'newB' and 'a' to
                # the 'a' to make 'a' and
                # 'newB' as same
                a = a + diff
 
                # Return side of the
                # square a or newB
                if (a == newB):
                    return a
                return 0
 
        # If a < b
        else:
             
            # Double the smaller value
            # that is 'a' and store
            # it to 'newA'
            newA = a + a
 
            # Find the difference of
            # 'newA and 'b'
            diff = abs(newA - b)
 
            # If 'newA' < b
            if (newA < b):
                 
                # Then add the difference
                # of 'newA' and 'b' to
                # the 'a' to make 'a'
                # and 'b' as same
                a = diff + newA
 
                # Return side of the
                # square a or b
                if (a == b):
                    return a
                return 0
             
            else:
                 
                # If 'newA' > b then
                # then add the difference
                # of 'newA' and 'b' to
                # the 'b' to make 'b' and
                # 'newA' as same
                b = b + diff
 
                # Return side of the
                # square b or newA
                if (b == newA):
                    return b
                return 0
 
# Driver code
 
# Size of rectangle
H = 3
W = 1
 
print(minimalSquareSide(H, W))
 
# This code is contributed by sanjoy_62


C#
// C# program of the above approach
using System;
 
class GFG{
     
public static int minimalSquareSide(int a, int b)
{
     
    // If 'a' and 'b' same
    // then double 'a' or 'b'
    // and return (2*a) or (2*b)
    if (a == b)
    {
        return 2 * a;
    }
     
    // Check if a!=b
    if (a != b)
    {
         
        // If a > b
        if (a > b)
        {
             
            // Double the smaller value
            // that is 'b' and store
            // it to 'newB'
            int newB = b + b;
     
            // Find the difference of
            // 'newB and 'a'
            int diff = Math.Abs(newB - a);
     
            // If 'newB' < a
            if (newB < a)
            {
                 
                // Then add the difference of
                // 'newB' and 'a' to the 'b'
                // to make 'b' and 'a' as same
                b = newB + diff;
     
                // Return side of the
                // square a or b
                if (a == b)
                    return a;
                return 0;
            }
            else
            {
                 
                // If 'newB' > a then
                // then add the difference
                // of 'newB' and 'a' to
                // the 'a' to make 'a' and
                // 'newB' as same
                a = a + diff;
     
                // Return side of the
                // square a or newB
                if (a == newB)
                    return a;
                return 0;
            }
        }
     
        // If a < b
        else
        {
             
            // Double the smaller value
            // that is 'a' and store
            // it to 'newA'
            int newA = a + a;
     
            // Find the difference of
            // 'newA and 'b'
            int diff = Math.Abs(newA - b);
     
            // If 'newA' < b
            if (newA < b)
            {
                 
                // Then add the difference
                // of 'newA' and 'b' to
                // the 'a' to make 'a'
                // and 'b' as same
                a = diff + newA;
     
                // Return side of the
                // square a or b
                if (a == b)
                    return a;
                return 0;
            }
            else
            {
                 
                // If 'newA' > b then
                // then add the difference
                // of 'newA' and 'b' to
                // the 'b' to make 'b' and
                // 'newA' as same
                b = b + diff;
     
                // Return side of the
                // square b or newA
                if (b == newA)
                    return b;
                return 0;
            }
        }
    }
    return 0;
}
 
// Driver code
public static void Main(String[] args)
{
    int H, W;
 
    // Size of rectangle
    H = 3; W = 1;
     
    Console.WriteLine(minimalSquareSide(H, W));
}
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:
3

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