给定一个尺寸为L和W的矩形。任务是找到一个矩形的最大面积,该矩形可以围绕一个给定的尺寸为L和W 的矩形。
例子:
Input: L = 10, W = 10
Output: 200
Input: L = 18, W = 12
Output: 450
方法:下面是尺寸L和W的给定矩形EFGH 。我们必须找到外接矩形EFGH的矩形ABCD的面积。
在上图中:
如果然后因为 GCF 是直角三角形。
所以,
=>
=>
相似地,
现在,矩形 ABCD 的面积由下式给出:
Area = AB * AD
Area = (AE + EB)*(AH + HD) …..(1)
According to the projection rule:
AE = L*sin(X)
EB = W*cos(X)
AH = L*cos(X)
HD = W*sin(X)
将上述投影的值代入等式(1),我们有:
Now to maximize the area, the value of sin(2X) must be maximum i.e., 1.
Therefore after substituting sin(2X) as 1 we have,
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find area of rectangle
// inscribed another rectangle of
// length L and width W
double AreaofRectangle(int L, int W)
{
// Area of rectangle
double area = (W + L) * (W + L) / 2;
// Return the area
return area;
}
// Driver Code
int main()
{
// Given dimensions
int L = 18;
int W = 12;
// Function call
cout << AreaofRectangle(L, W);
return 0;
}
// This code is contributed by Princi Singh
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function to find area of rectangle
// inscribed another rectangle of
// length L and width W
static double AreaofRectangle(int L, int W)
{
// Area of rectangle
double area = (W + L) * (W + L) / 2;
// Return the area
return area;
}
// Driver Code
public static void main(String args[])
{
// Given dimensions
int L = 18;
int W = 12;
// Function call
System.out.println(AreaofRectangle(L, W));
}
}
// This code is contributed by offbeat
Python3
# Python3 program for the above approach
# Function to find area of rectangle
# inscribed another rectangle of
# length L and width W
def AreaofRectangle(L, W):
# Area of rectangle
area =(W + L)*(W + L)/2
# Return the area
return area
# Driver Code
if __name__ == "__main__":
# Given Dimensions
L = 18
W = 12
# Function Call
print(AreaofRectangle(L, W))
C#
// C# program for the above approach
using System;
class GFG{
// Function to find area of rectangle
// inscribed another rectangle of
// length L and width W
static double AreaofRectangle(int L, int W)
{
// Area of rectangle
double area = (W + L) * (W + L) / 2;
// Return the area
return area;
}
// Driver Code
public static void Main(String []args)
{
// Given dimensions
int L = 18;
int W = 12;
// Function call
Console.Write(AreaofRectangle(L, W));
}
}
// This code is contributed by shivanisinghss2110
Javascript
输出:
450.0
时间复杂度: O(1)
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live