HDU-5858:Hard problem(相交圆面积)

来源:互联网 发布:淘宝售前客服 编辑:程序博客网 时间:2024/05/21 22:53

Hard problem

                                                                    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                                                                             Total Submission(s): 753    Accepted Submission(s): 481


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.
 

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
11
 

Sample Output
0.29

思路:利用模版求出半径为L的大圆的1/4与正方形内切圆的相交面积area,那么答案ans=2*(PI*L*L/4(内切圆的面积)-area)。

#include<bits/stdc++.h>using namespace std;const double PI=acos(-1.0);struct Point{    double x,y,r;}p[2];double dis(Point a,Point b){    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}double solve(Point a,Point b){    double d=dis(a,b);    if(d>=a.r+b.r)  return 0;    else if(d<=fabs(a.r-b.r))    {        double mn=min(a.r,b.r);        return PI*mn*mn;    }    double ang1=acos((a.r*a.r+d*d-b.r*b.r)/(2.0*d*a.r));    double ang2=acos((b.r*b.r+d*d-a.r*a.r)/(2.0*d*b.r));    double area=a.r*a.r*ang1+b.r*b.r*ang2-d*a.r*sin(ang1);    return area;}int main(){    int T;cin>>T;    while(T--)    {        double L;        cin>>L;        p[0].x=0;        p[0].y=L;        p[0].r=L;        p[1].x=L/2;        p[1].y=L/2;        p[1].r=L/2;        double area=solve(p[0],p[1]);        printf("%0.2lf\n",2*(PI*L*L/4-area));    }    return 0;}


原创粉丝点击