给定一个整数area ,任务是找到具有给定面积的矩形的长和宽,使得长和宽之间的差异尽可能小。
例子:
Input: area = 99
Output: l = 11, b = 9
All possible rectangles (l, b) are (99, 1), (33, 3) and (11, 9)
And the one with the minimum |l – b| is (11, 9)
Input: area = 25
Output: l = 5, b = 5
方法:任务是找到两个整数l和b ,使得l * b = 面积和|l – b|尽可能少。可以使用因式分解来解决该问题,但仅从1到N进行简单的因式分解将需要很长时间才能获得较大N值所需的输出。
为了克服这个,只需迭代到。考虑 < l ≤ 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
?>
Javascript
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.
Javascript
输出:
l = 11, b = 9
时间复杂度: O( )
下面是一个简单的实现。
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
蟒蛇3
# 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.
Javascript
输出:
11 9