题目大意:给你两个相同的圆环,求出它们相交的面积。输入数据为圆环内径,外径以及两个圆环的圆心坐标(以上数据均为整数)。
解题思路:两圆环相交面积 = 两个大圆相交面积 - 两倍大小圆相交面积 + 两个小圆相交面积
AC代码:
1 #include <cstdlib> 2 #include <iostream> 3 #include <cmath> 4 #include <cstring> 5 #include <algorithm> 6 #include <stdio.h> 7 using namespace std; 8 const double PI = acos(-1); 9 10 struct circle { 11 double x; 12 double y; 13 double r; 14 }; 15 double dist(circle a, circle b) 16 { 17 return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); 18 } 19 double area(circle a, circle b) { 20 21 if((dist(a, b)+min(a.r,b.r))<=max(a.r,b.r)) 22 { 23 if(a.r<b.r) 24 return PI*a.r*a.r; 25 else 26 return PI*b.r*b.r; 27 } 28 else if(dist(a, b)>=(a.r+b.r)) 29 return 0.0; 30 else 31 { 32 double length=dist(a, b); 33 double d1=2*acos((a.r*a.r+length*length-b.r*b.r)/(2*a.r*length)); 34 double d2=2*acos((b.r*b.r+length*length-a.r*a.r)/(2*b.r*length)); 35 double area1=a.r*a.r*d1/2-a.r*a.r*sin(d1)/2; 36 double area2=b.r*b.r*d2/2-b.r*b.r*sin(d2)/2; 37 double area=area1+area2; 38 return area; 39 } 40 } 41 int main() 42 { 43 int T; 44 circle a, b, A, B; 45 double r, R; 46 scanf("%d",&T); 47 for(int i=1; i<=T; i++) 48 { 49 scanf("%lf%lf", &r, &R); 50 a.r = b.r = r; 51 A.r = B.r = R; 52 scanf("%lf%lf", &a.x, &a.y); 53 A.x = a.x; 54 A.y = a.y; 55 scanf("%lf%lf", &b.x, &b.y); 56 B.x = b.x; 57 B.y = b.y; 58 double sec = area(A, B) - area(A, b) - area(B, a) + area(a, b); 59 printf("Case #%d: %.6lf ", i, sec); 60 61 } 62 63 return 0; 64 }
不知为何用Java代码会超时;但是思路还是一样的。