acdream:Andrew Stankevich Contest 3:Two Cylinders:数值积分
Two Cylinders
Special JudgeTime Limit: 10000/5000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others)
Problem Description
In this problem your task is very simple.
Consider two infinite cylinders in three-dimensional space, of radii R1 and R2 respectively, located in such a way that their axes intersect and are perpendicular.
Your task is to find the volume of their intersection.
Input
Input file contains two real numbers R1 and R2 (1 <= R1,R2 <= 100).
Output
Output the volume of the intersection of the cylinders. Your answer must be accurate up to 10-4.
Sample Input
1 1
Sample Output
5.3333
Source
Andrew Stankevich Contest 3
算法:先积分,再套用Simpson模板即可。
至于积分,就是高数的内容了,把2个圆柱中半径比较小的设为r1,半径较大的设为r2.
我们把小圆柱的轴线重叠z轴放置,大圆柱的轴线重叠y轴放置。
假设有一个平行于yOz平面的平面截得两圆柱的相交部分,得到一个截面,这个截面的面积是多少呢?
假设截面与x轴交于点(x,0,0),又因为是2个圆柱的相交部分,所以截面的一边长为2√(r12-x2)
同理,另一边长为2√(r22-x2)
最后得到一个积分:
8∫√(r12-x2)(r22-x2)dx
对[0,r1]求积分,就是结果。
直接积出来是很困难的,下面就是直接套Simpson模板了,写出全局函数F,其他没什么了。
1 #include<cstdio> 2 #include<cmath> 3 #include <algorithm> 4 using namespace std; 5 6 double r1,r2; 7 8 // simpson公式用到的函数,就是待积分函数 9 double F(double x) 10 { 11 return sqrt(r1*r1-x*x)*sqrt(r2*r2-x*x); 12 } 13 14 // 三点simpson法。这里要求F是一个全局函数 15 double simpson(double a, double b) 16 { 17 double c = a + (b-a)/2; 18 return (F(a)+4*F(c)+F(b))*(b-a)/6; 19 } 20 21 // 自适应Simpson公式(递归过程)。已知整个区间[a,b]上的三点simpson值A 22 double asr(double a, double b, double eps, double A) 23 { 24 double c = a + (b-a)/2; 25 double L = simpson(a, c), R = simpson(c, b); 26 if(fabs(L+R-A) <= 15*eps) return L+R+(L+R-A)/15.0; 27 return asr(a, c, eps/2, L) + asr(c, b, eps/2, R); 28 } 29 30 // 自适应Simpson公式(主过程) 31 double asr(double a, double b, double eps) 32 { 33 return asr(a, b, eps, simpson(a, b)); 34 } 35 36 // 用自适应Simpson公式计算积分数值 37 double getValue() 38 { 39 40 return asr(0, r1, 1e-5)*8; // 第一第二个参数为积分区间,第三个参数为精度 41 } 42 43 int main() 44 { 45 while(~scanf("%lf%lf",&r1,&r2)) 46 { 47 if(r1>r2) 48 swap(r1,r2); 49 printf("%.10f\n",getValue()); 50 } 51 return 0; 52 }