给定半径为r的半圆,我们必须找到可以在该半圆中内接的最大矩形,其底数位于直径上。
例子:
Input : r = 4
Output : 16
Input : r = 5
Output :25
令r为半圆的半径, x为矩形底边的一半, y为矩形的高度。我们要最大化面积,A = 2xy。
因此,从图中我们可以看到,
y =√(r ^ 2 – x ^ 2)
因此, A = 2 * x *(√(r ^ 2 – x ^ 2))或dA / dx = 2 *√(r ^ 2 – x ^ 2)-2 * x ^ 2 /√(r ^ 2 – x ^ 2)
将此导数设置为0并求解x,
dA / dx = 0
或2 *√(r ^ 2 – x ^ 2)– 2 * x ^ 2 /√(r ^ 2 – x ^ 2)= 0
2r ^ 2 – 4x ^ 2 = 0
x = r /√2
这是该区域的最大值,因为
当x> r /√2时dA / dx> 0
并且,当x> r /√2时dA / dx <0
由于y =√(r ^ 2 – x ^ 2)
y = r /√2
因此,矩形的底部的长度为r /√2 ,其高度的长度为√2* r / 2 。
因此,面积, A = r ^ 2
C++
// C++ Program to find the
// the biggest rectangle
// which can be inscribed
// within the semicircle
#include
using namespace std;
// Function to find the area
// of the biggest rectangle
float rectanglearea(float r)
{
// the radius cannot be negative
if (r < 0)
return -1;
// area of the rectangle
float a = r * r;
return a;
}
// Driver code
int main()
{
float r = 5;
cout << rectanglearea(r) << endl;
return 0;
}
Java
// Java Program to find the
// the biggest rectangle
// which can be inscribed
// within the semicircle
class GFG
{
// Function to find the area
// of the biggest rectangle
static float rectanglearea(float r)
{
// the radius cannot be negative
if (r < 0)
return -1;
// area of the rectangle
float a = r * r;
return a;
}
// Driver code
public static void main(String[] args)
{
float r = 5;
System.out.println((int)rectanglearea(r));
}
}
// This code is contributed
// by ChitraNayal
Python 3
# Python 3 Program to find the
# the biggest rectangle
# which can be inscribed
# within the semicircle
# Function to find the area
# of the biggest rectangle
def rectanglearea(r) :
# the radius cannot
# be negative
if r < 0 :
return -1
# area of the rectangle
a = r * r
return a
# Driver Code
if __name__ == "__main__" :
r = 5
# function calling
print(rectanglearea(r))
# This code is contributed
# by ANKITRAI1
C#
// C# Program to find the
// the biggest rectangle
// which can be inscribed
// within the semicircle
using System;
class GFG
{
// Function to find the area
// of the biggest rectangle
static float rectanglearea(float r)
{
// the radius cannot be negative
if (r < 0)
return -1;
// area of the rectangle
float a = r * r;
return a;
}
// Driver code
public static void Main()
{
float r = 5;
Console.Write((int)rectanglearea(r));
}
}
// This code is contributed
// by ChitraNayal
PHP
Javascript
输出 :
25