给定一个正方形a的边,它保持在一个圆内。它不断扩大,直到它的四个顶点都接触到圆的圆周。另一个较小的圆圈现在保留在正方形内,并不断扩大,直到其圆周接触正方形的所有四个边。外圈和内圈形成一个环。找到这个阴影部分的区域,如下图所示。
例子:
Input: a = 3
Output: 7.06858
Input: a = 4
Output: 12.566371
方法:
从上图中可以得出R = a / sqrt(2)其中a是正方形的边长。外圆的面积是(pi * R * R) 。
让s1是外圆的面积(pi * R * R)和s2是内圆的面积(pi * r * r) 。那么环的面积是s1 – s2 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the required area
float getArea(int a)
{
// Calculate the area
float area = (M_PI * a * a) / 4.0;
return area;
}
// Driver code
int main()
{
int a = 3;
cout << getArea(a);
return 0;
}
Java
// Java implementation of the approach
class GFG {
// Function to return the required area
static float getArea(int a)
{
// Calculate the area
float area = (float)(Math.PI * a * a) / 4;
return area;
}
// Driver code
public static void main(String args[])
{
int a = 3;
System.out.println(getArea(a));
}
}
Python3
# Python3 implementation of the approach
import math
# Function to return the required area
def getArea(a):
# Calculate the area
area = (math.pi * a * a) / 4
return area
# Driver code
a = 3
print('{0:.6f}'.format(getArea(a)))
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the required area
static float getArea(int a)
{
// Calculate the area
float area = (float)(Math.PI * a * a) / 4;
return area;
}
// Driver code
public static void Main()
{
int a = 3;
Console.Write(getArea(a));
}
}
// This code is contributed by mohit kumar 29
Javascript
输出:
7.06858