hdu 1724 Ellipse (辛普森积分)
Problem Description
Math is important!! Many students failed in 2+2’s mathematical test, so let's AC this problem to mourn for our lost youth..
Look this sample picture:
A ellipses in the plane and center in point O. the L,R lines will be vertical through the X-axis. The problem is calculating the blue intersection area. But calculating the intersection area is dull, so I have turn to you, a talent of programmer. Your task is tell me the result of calculations.(defined PI=3.14159265 , The area of an ellipse A=PI*a*b )
Look this sample picture:
A ellipses in the plane and center in point O. the L,R lines will be vertical through the X-axis. The problem is calculating the blue intersection area. But calculating the intersection area is dull, so I have turn to you, a talent of programmer. Your task is tell me the result of calculations.(defined PI=3.14159265 , The area of an ellipse A=PI*a*b )
Input
Input may contain multiple test cases. The first line is a positive integer N, denoting the number of test cases below. One case One line. The line will consist of a pair of integers a and b, denoting the ellipse equation , A pair of integers l and r, mean the L is (l, 0) and R is (r, 0). (-a <= l <= r <= a).
Output
For each case, output one line containing a float, the area of the intersection, accurate to three decimals after the decimal point.
Sample Input
2
2 1 -2
2
2 1 0 2
Sample Output
6.283
3.142
求一个椭圆[l,r]之间的面积
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 int t; 5 const double esp = 1e-10; 6 double a,b; 7 double f (double x) 8 { 9 return b*sqrt(1.0-(x*x)/(a*a)); 10 } 11 double simpson (double l,double r) 12 { 13 return (f(l)+4*f((l+r)/2.0)+f(r))/6.0*(r-l); 14 } 15 double integral (double l,double r) 16 { 17 double mid = (l+r)/2.0; 18 double res = simpson(l,r); 19 if (fabs(res-simpson(l,mid)-simpson(mid,r)) < esp) 20 return res; 21 else 22 return integral(l,mid) + integral(mid,r); 23 } 24 int main() 25 { 26 //freopen("de.txt","r",stdin); 27 scanf("%d",&t); 28 while (t--){ 29 double l,r; 30 cin>>a>>b>>l>>r; 31 printf("%.3lf\n",integral(l,r)*2); 32 } 33 return 0; 34 }