📌  相关文章
📜  可内接于半圆的最大矩形

📅  最后修改于: 2021-10-23 08:41:14             🧑  作者: Mango

给定半径为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