217. Two Cylinders
time limit per test: 0.5 sec.
memory limit per test: 65536 KB
memory limit per test: 65536 KB
input: standard
output: standard
output: standard
In this problem your task is very simple.
Consider two infinite cylinders in three-dimensional space, of radii R 1 and R 2 respectively, located in such a way that their axes intersect and are perpendicular.
Your task is to find the volume of their intersection.
Consider two infinite cylinders in three-dimensional space, of radii R 1 and R 2 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 R 1 and R 2 (1 ≤ R 1, R 2 ≤ 100);
Output
Output the volume of the intersection of the cylinders. Your answer must be accurate up to 10 -4.
Sample test(s)
Input
1 1
Output
5.3333
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
double r1, r2;
double f(double x){
return sqrt((r1*r1-x*x)*(r2*r2-x*x));//写要求辛普森积分的函数
}
double simpson(double L, double R){//三点辛普森积分法,要求f(x)是全局函数
double mid = (L + R) / 2.0;
return (f(L) + 4.0 * f(mid) + f(R)) * (R - L) / 6.0;
}
double integral(double L, double R, double Eps){//自适应辛普森积分递归过程
double mid = (L + R) / 2.0;
double ST = simpson(L, R), SL = simpson(L, mid), SR = simpson(mid, R);
if(fabs(SL + SR - ST) <= 15.0 * Eps) return SL + SR + (SL + SR - ST) / 15.0;//直接返回结果
return integral(L, mid, Eps/2.0) + integral(mid, R, Eps/2.0);//对半划分区间
}
int main()
{
cin>>r1>>r2;
printf("%lf",integral(0,min(r1,r2),1e-4)*8);
return 0;
}