Two Cylinders
Special JudgeTime Limit: 10000/5000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others)
SubmitStatisticNext Problem
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,其他没什么了。
#include<cstdio>
#include<cmath>
#include <algorithm>
using namespace std; double r1,r2; // simpson公式用到的函数,就是待积分函数
double F(double x)
{
return sqrt(r1*r1-x*x)*sqrt(r2*r2-x*x);
} // 三点simpson法。这里要求F是一个全局函数
double simpson(double a, double b)
{
double c = a + (b-a)/;
return (F(a)+*F(c)+F(b))*(b-a)/;
} // 自适应Simpson公式(递归过程)。已知整个区间[a,b]上的三点simpson值A
double asr(double a, double b, double eps, double A)
{
double c = a + (b-a)/;
double L = simpson(a, c), R = simpson(c, b);
if(fabs(L+R-A) <= *eps) return L+R+(L+R-A)/15.0;
return asr(a, c, eps/, L) + asr(c, b, eps/, R);
} // 自适应Simpson公式(主过程)
double asr(double a, double b, double eps)
{
return asr(a, b, eps, simpson(a, b));
} // 用自适应Simpson公式计算积分数值
double getValue()
{ return asr(, r1, 1e-)*; // 第一第二个参数为积分区间,第三个参数为精度
} int main()
{
while(~scanf("%lf%lf",&r1,&r2))
{
if(r1>r2)
swap(r1,r2);
printf("%.10f\n",getValue());
}
return ;
}