给出一个长l宽b的矩形,它内接一个菱形,菱形又内接一个圆。任务是找到这个圆的半径。
例子:
Input: l = 5, b = 3
Output: 1.28624
Input: l = 6, b = 4
Output: 1.6641
方法:从图中可以看出,对角线x & y等于矩形的长和宽。
此外,菱形内的圆半径r为 = xy/2√(x^2+y^2)。
因此,根据l & b的圆半径是 = lb/2√(l^2+b^2)。
下面是上述方法的实现:
C++
// C++ implementation of above approach
#include
using namespace std;
// Function to find the radius
// of the inscribed circle
float circleradius(float l, float b)
{
// the sides cannot be negative
if (l < 0 || b < 0)
return -1;
// radius of the circle
float r = (l * b) / (2 * sqrt((pow(l, 2) + pow(b, 2))));
return r;
}
// Driver code
int main()
{
float l = 5, b = 3;
cout << circleradius(l, b) << endl;
return 0;
}
Java
// Java implementation of above approach
import java.io.*;
class GFG {
// Function to find the radius
// of the inscribed circle
static float circleradius(float l, float b)
{
// the sides cannot be negative
if (l < 0 || b < 0)
return -1;
// radius of the circle
float r = (float)((l * b) / (2 * Math.sqrt((Math.pow(l, 2) + Math.pow(b, 2)))));
return r;
}
// Driver code
public static void main (String[] args) {
float l = 5, b = 3;
System.out.print (circleradius(l, b)) ;
}
}
// This code is contributed by inder_verma..
Python3
# Python 3 implementation of
# above approach
from math import sqrt
# Function to find the radius
# of the inscribed circle
def circleradius(l, b):
# the sides cannot be negative
if (l < 0 or b < 0):
return -1
# radius of the circle
r = (l * b) / (2 * sqrt((pow(l, 2) +
pow(b, 2))));
return r
# Driver code
if __name__ == '__main__':
l = 5
b = 3
print("{0:.5}" . format(circleradius(l, b)))
# This code is contribute
# by Surendra_Gagwar
C#
// C# implementation of above approach
using System;
class GFG
{
// Function to find the radius
// of the inscribed circle
static float circleradius(float l,
float b)
{
// the sides cannot be negative
if (l < 0 || b < 0)
return -1;
// radius of the circle
float r = (float)((l * b) /
(2 * Math.Sqrt((Math.Pow(l, 2) +
Math.Pow(b, 2)))));
return r;
}
// Driver code
public static void Main ()
{
float l = 5, b = 3;
Console.WriteLine(circleradius(l, b));
}
}
// This code is contributed
// by inder_verma
PHP
Javascript
输出:
1.28624
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。