📌  相关文章
📜  可以刻在半圆上的最大正方形

📅  最后修改于: 2021-05-04 10:43:21             🧑  作者: 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