📌  相关文章
📜  矩形,长度和宽度之间的差异尽可能小

📅  最后修改于: 2021-04-29 18:41:08             🧑  作者: Mango

给定整数区域,任务是找到具有给定面积的矩形的长度和宽度,以使长度和宽度之间的差异最小。
例子:

方法:任务是找到两个整数lb ,使得l * b =面积| l – b |尽可能最小。可以使用分解来解决问题,但是仅执行从1N的简单分解将需要很长时间才能获得较大值N的所需输出。
为了克服这个问题,只需迭代到      。考虑中      ,则对于所有l值, b始终为<
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to print the length (l)
// and breadth (b) of the rectangle
// having area = N and |l - b| as
// minimum as possible
void find_rectangle(int area)
{
    int l, b;
    int M = sqrt(area), ans;
 
    for (int i = M; i >= 1; i--) {
 
        // i is a factor
        if (area % i == 0) {
 
            // l >= sqrt(area) >= i
            l = (area / i);
 
            // so here l is +ve always
            b = i;
            break;
        }
    }
 
    // Here l and b are length and
    // breadth of the rectangle
    cout << "l = " << l << ", b = "
         << b << endl;
}
 
// Driver code
int main()
{
    int area = 99;
    find_rectangle(area);
    return 0;
}


Java
// Java implementation of the approach
class GFG {
 
    // Function to print the length (l)
    // and breadth (b) of the rectangle
    // having area = N and |l - b| as
    // minimum as possible
    static void find_rectangle(int area)
    {
        int l = 0, b = 0;
        int M = (int)Math.sqrt(area), ans;
 
        for (int i = M; i >= 1; i--) {
 
            // i is a factor
            if (area % i == 0) {
 
                // l >= sqrt(area) >= i
                l = (area / i);
 
                // so here l is +ve always
                b = i;
                break;
            }
        }
 
        // Here l and b are length and
        // breadth of the rectangle
        System.out.println("l = " + l + ", b = " + b);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int area = 99;
        find_rectangle(area);
    }
}
 
// This code is contributed by Ita_c.


Python3
# Python3 implementation of the approach
import math as mt
 
# Function to print the length (l)
# and breadth (b) of the rectangle
# having area = N and |l - b| as
# minimum as possible
def find_rectangle(area):
 
    l, b = 0, 0
    M = mt.ceil(mt.sqrt(area))
    ans = 0
 
    for i in range(M, 0, -1):
 
        # i is a factor
        if (area % i == 0):
 
            # l >= sqrt(area) >= i
            l = (area // i)
 
            # so here l is + ve always
            b = i
            break
         
    # Here l and b are length and
    # breadth of the rectangle
    print("l =", l, ", b =", b)
 
# Driver code
area = 99
find_rectangle(area)
 
# This code is contributed by
# Mohit kumar 29


C#
// C# implementation of the approach
using System;
class GFG {
 
    // Function to print the length (l)
    // and breadth (b) of the rectangle
    // having area = N and |l - b| as
    // minimum as possible
    static void find_rectangle(int area)
    {
        int l = 0, b = 0;
        int M = (int)Math.Sqrt(area);
 
        for (int i = M; i >= 1; i--) {
 
            // i is a factor
            if (area % i == 0) {
 
                // l >= sqrt(area) >= i
                l = (area / i);
 
                // so here l is +ve always
                b = i;
                break;
            }
        }
 
        // Here l and b are length and
        // breadth of the rectangle
        Console.WriteLine("l = " + l + ", b = " + b);
    }
 
    // Driver code
    public static void Main()
    {
        int area = 99;
        find_rectangle(area);
    }
}
 
// This code is contributed by Mukul Singh.


PHP
= 1; $i--)
    {
 
        // i is a factor
        if ($area % $i == 0)
        {
 
            // l >= sqrt(area) >= i
            $l = floor($area / $i);
 
            // so here l is +ve always
            $b = $i ;
            break;
        }
    }
 
    // Here l and b are length and
    // breadth of the rectangle
    echo "l = ", $l, ", b = ", $b, "\n";
}
 
// Driver code
$area = 99;
find_rectangle($area);
 
// This code is contributed by Ryuga
?>


CPP
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to print the length (l)
// and breadth (b) of the rectangle
// having area = N and |l - b| as
// minimum as possible
void find_rectangle(int area)
{
    for (int i = ceil(sqrt(area)); i <= area; i++) {
        if (area / i * i == area) {
            printf("%d %d", i, area / i);
            return;
        }
    }
}
 
// Driver code
int main()
{
    int area = 99;
    find_rectangle(area);
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
class GFG {
    // Function to print the length (l)
    // and breadth (b) of the rectangle
    // having area = N and |l - b| as
    // minimum as possible
    static void find_rectangle(int area)
    {
        for(int i = (int)Math.ceil(Math.sqrt(area)); i <= area; i++)
        {
            if(area / i * i == area)
            {
                System.out.println(i + " " + (int)(area / i));
                return;
            }
        }
    }
   
    // Driver code
    public static void main (String[] args)
    {
        int area = 99;
        find_rectangle(area);       
    }
}
 
// This code is contributed by rag2127


Python3
# Python3 implementation of the approach
import math
 
# Function to print the length (l)
# and breadth (b) of the rectangle
# having area = N and |l - b| as
# minimum as possible
def find_rectangle(area):
    for i in range(int(math.ceil(math.sqrt(area))) , area + 1):
        if((int(area / i) * i) == area):
            print(i, int(area / i))
            return
 
# Driver code
area = 99
find_rectangle(area)
 
# This code is contributed by avanitrachhadiya2155


C#
// C# implementation of the approach
using System;
class GFG
{
 
  // Function to print the length (l)
  // and breadth (b) of the rectangle
  // having area = N and |l - b| as
  // minimum as possible
  static void find_rectangle(int area)
  {
    for(int i = (int)Math.Ceiling(Math.Sqrt(area)); i <= area; i++)
    {
      if(area / i * i == area)
      {
        Console.WriteLine(i + " " + (int)(area / i));
        return;
      }
    }
  }
 
  // Driver code
  static void Main()
  {
    int area = 99;
    find_rectangle(area);
  }
}
 
// This code is contributed by divyeshrabadiya07.


输出:
l = 11, b = 9

时间复杂度: O( \sqrt{N}       )
下面是简单的实现。

CPP

// C++ implementation of the approach
#include 
using namespace std;
 
// Function to print the length (l)
// and breadth (b) of the rectangle
// having area = N and |l - b| as
// minimum as possible
void find_rectangle(int area)
{
    for (int i = ceil(sqrt(area)); i <= area; i++) {
        if (area / i * i == area) {
            printf("%d %d", i, area / i);
            return;
        }
    }
}
 
// Driver code
int main()
{
    int area = 99;
    find_rectangle(area);
    return 0;
}

Java

// Java implementation of the approach
import java.io.*;
class GFG {
    // Function to print the length (l)
    // and breadth (b) of the rectangle
    // having area = N and |l - b| as
    // minimum as possible
    static void find_rectangle(int area)
    {
        for(int i = (int)Math.ceil(Math.sqrt(area)); i <= area; i++)
        {
            if(area / i * i == area)
            {
                System.out.println(i + " " + (int)(area / i));
                return;
            }
        }
    }
   
    // Driver code
    public static void main (String[] args)
    {
        int area = 99;
        find_rectangle(area);       
    }
}
 
// This code is contributed by rag2127

Python3

# Python3 implementation of the approach
import math
 
# Function to print the length (l)
# and breadth (b) of the rectangle
# having area = N and |l - b| as
# minimum as possible
def find_rectangle(area):
    for i in range(int(math.ceil(math.sqrt(area))) , area + 1):
        if((int(area / i) * i) == area):
            print(i, int(area / i))
            return
 
# Driver code
area = 99
find_rectangle(area)
 
# This code is contributed by avanitrachhadiya2155

C#

// C# implementation of the approach
using System;
class GFG
{
 
  // Function to print the length (l)
  // and breadth (b) of the rectangle
  // having area = N and |l - b| as
  // minimum as possible
  static void find_rectangle(int area)
  {
    for(int i = (int)Math.Ceiling(Math.Sqrt(area)); i <= area; i++)
    {
      if(area / i * i == area)
      {
        Console.WriteLine(i + " " + (int)(area / i));
        return;
      }
    }
  }
 
  // Driver code
  static void Main()
  {
    int area = 99;
    find_rectangle(area);
  }
}
 
// This code is contributed by divyeshrabadiya07.
输出:
11 9