📜  能内接成半圆的最大正方形

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

给定一个半径为r的半圆,我们必须找到可以内接在半圆上的最大正方形,底边位于直径上。
例子:

Input: r = 5
Output: 20

Input: r = 8
Output: 51.2

方法:设r为半圆的半径,a 为正方形的边长。
从图中我们可以看出,圆心也是正方形底边的中点。所以在直角三角形AOB中,由毕达哥拉斯定理

下面是上述方法的实现

C++
// C++ Program to find the biggest square
// which can be inscribed within the semicircle
#include 
using namespace std;
 
// Function to find the area
// of the squaare
float squarearea(float r)
{
 
    // the radius cannot be negative
    if (r < 0)
        return -1;
 
    // area of the square
    float a = 4 * (pow(r, 2) / 5);
 
    return a;
}
 
// Driver code
int main()
{
    float r = 5;
    cout << squarearea(r) << endl;
 
    return 0;
}


Java
// Java Program to find the biggest square
// which can be inscribed within the semicircle
 
import java.io.*;
 
class GFG {
 
 
// Function to find the area
// of the squaare
static float squarearea(float r)
{
 
    // the radius cannot be negative
    if (r < 0)
        return -1;
 
    // area of the square
    float a = 4 * (float)(Math.pow(r, 2) / 5);
 
    return a;
}
 
// Driver code
 
    public static void main (String[] args) {
         float r = 5;
    System.out.println( squarearea(r));
    }
}
// This code is contributed by chandan_jnu.


Python3
# Python 3 program to find the
# biggest square which can be
# inscribed within the semicircle
 
# Function to find the area
# of the squaare
def squarearea(r):
 
    # the radius cannot be
    # negative
    if (r < 0):
        return -1
 
    # area of the square
    a = 4 * (pow(r, 2) / 5)
 
    return a
 
# Driver code
if __name__ == "__main__":
     
    r = 5
    print(int(squarearea(r)))
 
# This code is contributed
# by ChitraNayal


C#
// C# Program to find the
// biggest square which can be
// inscribed within the semicircle
using System;
 
class GFG
{
 
// Function to find the
// area of the squaare
static float squarearea(float r)
{
 
    // the radius cannot be negative
    if (r < 0)
        return -1;
 
    // area of the square
    float a = 4 * (float)(Math.Pow(r, 2) / 5);
 
    return a;
}
 
// Driver code
public static void Main ()
{
    float r = 5;
    Console.WriteLine(squarearea(r));
}
}
 
// This code is contributed
// by anuj_67


PHP


Javascript


输出:
20

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程