[TIOJ] 1846. 周強的試煉

題目連結:http://tioj.infor.org/problems/1846
想了一段時間才想出來,我的數學一定是太爛了QQ。做法就是先用圓心距跟兩個半徑套餘弦定理求出兩個扇形的夾角,那答案就是兩個扇形的面積扣掉兩組半徑所構成的箏形面積,此外記得多判一下兩園沒交點的情況。
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);

inline double dis(double,double,double,double);

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	int t; cin>>t;
	while(t--){
		int x1, y1, r1; cin>>x1>>y1>>r1;
		int x2, y2, r2; cin>>x2>>y2>>r2;
		double dd = dis(x1, y1, x2, y2);
		if(dd >= r1+r2) cout<<"0.00\n";
		else if(dd+min(r1, r2) <= max(r1, r2))
			cout<<fixed<<setprecision(2)<<min(r1, r2)*min(r1, r2)*PI<<'\n';
		else{
			double deg1 = acos((dd*dd+r1*r1-r2*r2)/(2*dd*r1));
			double deg2 = acos((dd*dd+r2*r2-r1*r1)/(2*dd*r2));
			double ans = r1*r1*deg1+r2*r2*deg2 - r1*r1*sin(deg1*2)/2 - r2*r2*sin(deg2*2)/2;
			cout<<fixed<<setprecision(2)<<ans<<'\n';
		}
	}
	return 0;
}

inline double dis(double x1, double y1, double x2, double y2){
	return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

留言

這個網誌中的熱門文章

[TIOJ] 1094. C.幼稚國王的獎賞

[Codeforces] 731F. Video Cards

[IOJ] 19. 啦啦啦