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

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

给定一个长度为l和宽度为b的矩形,我们必须找到可以内接在该矩形中的最大圆。
例子:

Input  : l = 4, b = 8
Output : 12.56

Input  : l = 16 b = 6
Output : 28.26

从图中我们可以看出,矩形内可内切的最大圆的半径总是等于矩形短边的一半。所以从图中,

C++
// C++ Program to find the biggest circle
// which can be inscribed  within the rectangle
#include 
using namespace std;
 
// Function to find the area
// of the biggest circle
float circlearea(float l, float b)
{
 
    // the length and breadth cannot be negative
    if (l < 0 || b < 0)
        return -1;
 
    // area of the circle
    if (l < b)
        return 3.14 * pow(l / 2, 2);
    else
        return 3.14 * pow(b / 2, 2);
}
 
// Driver code
int main()
{
    float l = 4, b = 8;
    cout << circlearea(l, b) << endl;
    return 0;
}


Java
// Java Program to find the
// biggest circle which can be
// inscribed within the rectangle
 
class GFG
{
 
// Function to find the area
// of the biggest circle
static float circlearea(float l,
                        float b)
{
 
// the length and breadth
// cannot be negative
if (l < 0 || b < 0)
    return -1;
 
// area of the circle
if (l < b)
    return (float)(3.14 * Math.pow(l / 2, 2));
else
    return (float)(3.14 * Math.pow(b / 2, 2));
}
 
// Driver code
public static void main(String[] args)
{
    float l = 4, b = 8;
    System.out.println(circlearea(l, b));
}
}
 
// This code is contributed
// by ChitraNayal


Python 3
# Python 3 Program to find the
# biggest circle which can be
# inscribed within the rectangle
 
# Function to find the area
# of the biggest circle
def circlearea(l, b):
 
    # the length and breadth
    # cannot be negative
    if (l < 0 or b < 0):
        return -1
 
    # area of the circle
    if (l < b):
        return 3.14 * pow(l // 2, 2)
    else:
        return 3.14 * pow(b // 2, 2)
 
# Driver code
if __name__ == "__main__":
    l = 4
    b = 8
    print(circlearea(l, b))
 
# This code is contributed
# by ChitraNayal


C#
// C# Program to find the
// biggest circle which can be
// inscribed within the rectangle
using System;
 
class GFG
{
 
// Function to find the area
// of the biggest circle
static float circlearea(float l,
                        float b)
{
 
// the length and breadth
// cannot be negative
if (l < 0 || b < 0)
    return -1;
 
// area of the circle
if (l < b)
    return (float)(3.14 * Math.Pow(l / 2, 2));
else
    return (float)(3.14 * Math.Pow(b / 2, 2));
}
 
// Driver code
public static void Main()
{
    float l = 4, b = 8;
    Console.Write(circlearea(l, b));
}
}
 
// This code is contributed
// by ChitraNayal


PHP


Javascript


输出:
12.56

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