sgu 217. Two Cylinders 積分

217. Two Cylinders

time limit per test: 0.5 sec.
memory limit per test: 65536 KB
input: standard
output: standard



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 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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章