📜  可以刻在半圆上的最大梯形

📅  最后修改于: 2021-04-27 17:29:04             🧑  作者: Mango

给定半径为r的半圆,任务是找到可刻在半圆上的最大梯形,其底面位于直径上。
例子:

Input: r = 5
Output: 32.476

Input: r = 8
Output: 83.1384

方法:令r为半圆的半径, x为梯形的下边缘, y上边缘,& h为梯形的高度。
现在从图中,

下面是上述方法的实现

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


Java
// Java Program to find the biggest trapezoid
// which can be inscribed within the semicircle
 
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG{
// Function to find the area
// of the biggest trapezoid
static float trapezoidarea(float r)
{
 
    // the radius cannot be negative
    if (r < 0)
        return -1;
 
    // area of the trapezoid
    float a = (3 * (float)Math.sqrt(3)
            * (float)Math.pow(r, 2)) / 4;
 
    return a;
}
 
// Driver code
public static void main(String args[])
{
    float r = 5;
    System.out.printf("%.3f",trapezoidarea(r));
}
}


Python 3
# Python 3 Program to find the biggest trapezoid
# which can be inscribed within the semicircle
 
# from math import everything
from math import *
 
# Function to find the area
# of the biggest trapezoid
def trapezoidarea(r) :
 
    # the radius cannot be negative
    if r < 0 :
        return -1
 
    # area of the trapezoid
    a = (3 * sqrt(3) * pow(r,2)) / 4
 
    return a
 
 
# Driver code    
if __name__ == "__main__" :
 
    r = 5
 
    print(round(trapezoidarea(r),3))
 
 
# This code is contributed by ANKITRAI1


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


PHP


Javascript


输出:
32.476