# hdu 5120 - Intersection

Posted by 111qqz on Thursday, February 19, 2015

## TOC

这题不难…倒是从学长那里收获了几点关于代码规范的问题…

——————————————————————————————————————————————

“就是一个函数声明为pow(double, double)你必须传两个double参数进去。但你传int也可以，int会转型会double，但c++有重载。声明了两个函数pow(double, double)，pow(long long, double),你传两个int进去编译器不知道把int转为double还是转为long long”
“解决办法是把int转型成double (xxx) 或者long long (xxx)” 也可以 简单粗暴的xxx.0

(int,int)->(double,int)?(double,double)

** **

``````#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

int t,tt;
int rr,RR,x11,x22,y11,y22;
double ans;
const double PI=acos(-1);
const double C=10e-6;
double area(int x1,int y1,int x2,int y2,int R,int r);
int main()
{
cin>>t;
tt=t;
while (t--)
{
cin>>rr>>RR;
cin>>x11>>y11>>x22>>y22;
ans=area(x11,y11,x22,y22,RR,RR)-2*area(x11,y11,x22,y22,RR,rr)+
area(x11,y11,x22,y22,rr,rr);
cout<<"Case #"<<tt-t<<": "
<<fixed<<setprecision(6)<<ans<<endl;
}
return 0;
}
double area(int x1,int y1,int x2,int y2,int R,int r)
{
double d;
double A,a;
double st;
d=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
if (d>=r+R)
return 0;
if (R-r>=d)
return r*r*PI;
A=acos((R*R+d*d-r*r)/(2.0*d*R));
a=acos((r*r+d*d-R*R)/(2.0*d*r));
st=R*d*sin(A);
A=A*2;
a=a*2;
return  (A*R*R+a*r*r)/2.0-st;
}
``````

「真诚赞赏，手留余香」