Hard problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 906 Accepted Submission(s): 554
Problem Description
cjj is fun with math problem. One day he found a Olympic Mathematics problem for primary school students. It is too difficult for cjj. Can you solve it?
Give you the side length of the square L, you need to calculate the shaded area in the picture.
The full circle is the inscribed circle of the square, and the center of two quarter circle is the vertex of square, and its radius is the length of the square.
![](https://i-blog.csdnimg.cn/blog_migrate/d8740ee759f31eb99b24e8d7a5c42d78.jpeg)
Give you the side length of the square L, you need to calculate the shaded area in the picture.
The full circle is the inscribed circle of the square, and the center of two quarter circle is the vertex of square, and its radius is the length of the square.
Input
The first line contains a integer T(1<=T<=10000), means the number of the test case. Each case contains one line with integer l(1<=l<=10000).
Output
For each test case, print one line, the shade area in the picture. The answer is round to two digit.
Sample Input
1 1
Sample Output
0.29
Author
BUPT
Source
Recommend
wange2014
思路:
先求出半径为d的大圆与半径为d/2的小圆相交的面积,用小圆的面积减去相交的面积是一块阴影部分的面积,最后乘以2即可。
还可以做几条辅助线求出一些角度,直接计算。
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
const double eps = 1e-8;
const double PI = acos(-1.0);
struct Point
{
double x,y;
Point(){};
Point(double _x,double _y)
{
x=_x; y=_y;
}
Point operator -(const Point &b)const
{
return Point(x-b.x,y-b.y);
}
double operator *(const Point &b)const
{
return x*b.x+y*b.y;
}
void transXY(double B)
{
double tx = x,ty=y;
x=tx*cos(B)-ty*sin(B);
y=tx*sin(B)+ty*cos(B);
}
};
double dist(Point a,Point b)
{
return sqrt((a-b)*(a-b));
}
double Area_of_overlap(Point c1,double r1,Point c2,double r2)
{
double d=dist(c1,c2);
if(r1+r2<d+eps) return 0;
if(d<fabs(r1-r2)+eps)
{
double r=min(r1,r2);
return PI*r*r;
}
double x = (d*d+r1*r1-r2*r2)/(2*d);
double t1=acos(x/r1);
double t2=acos((d-x)/r2);
return r1*r1*t1+r2*r2*t2-d*r1*sin(t1);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
double d;
scanf("%lf",&d);
Point a = Point(0,0);
Point b = Point(d/2,d/2);
printf("%.2f\n",2*(PI*d*d/4-Area_of_overlap(a,d,b,d/2)));
}
return 0;
}