Two Cylinders (辛普森公式处理积分)

Two Cylinders (辛普森公式处理积分):http://acm.hust.edu.cn/vjudge/contest/view.action?cid=120113#problem/I   传送门:nefu


题面描述:

I - Two Cylinders
Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu
Submit  Status

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 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 Input

1 1

Sample Output

5.3333


题目大意:

此题要计算两个圆柱体垂直相交之后形成的体积,且两个圆柱体的半径不一样。


题目分析:

可以先依据两个半径相等的圆柱体相交所得物体(称为:牟合方盖)的体积计算方法来推导半径不相等的物体的体积,计算牟合方盖的体积时,我们都是利用定积分,先计算在第一卦限中的体积,最后乘以8就行了,具体过程如下。


求解两柱体x2+y2=R2和x2+z2=R2相交部分的体积。

Two Cylinders (辛普森公式处理积分)_第1张图片Two Cylinders (辛普森公式处理积分)_第2张图片Two Cylinders (辛普森公式处理积分)_第3张图片



Two Cylinders (辛普森公式处理积分)_第4张图片


Two Cylinders (辛普森公式处理积分)_第5张图片




而此题所要求的是柱体x2+y2=R22和x2+z2=R12相交的体积。


我们同样也是只算第一卦限的体积,最后乘以8即可。



Two Cylinders (辛普森公式处理积分)_第6张图片



代码实现如下:

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

const double esp=1e-10;
double r1,r2;
double f(double x)
{
    return sqrt((r1*r1-x*x)*(r2*r2-x*x));
}

double Simpson(double a,double b)  
{
    return (b-a)*(f(a)+f(b)+4*f((a+b)/2.0))/6.0;
}

double Midd(double a,double b) ///迭代求解数值积分
{
    double mid=(a+b)/2.0;
    double res=Simpson(a,b);
    if(fabs(res-Simpson(a,mid)-Simpson(mid,b))<esp)
    {
        return res;
    }
    else return Midd(a,mid)+Midd(mid,b);
}

int main()
{

    while(scanf("%lf%lf",&r1,&r2)!=EOF)
    {
        if(r1==r2)
            printf("%.4lf\n",16.0/3*r1*r1*r1);
        else
        {
            if(r1>r2)
            {
                double t=r1;
                r1=r2;
                r2=t;
            }
            printf("%.4lf\n",8.0*Midd(0,r1));
        }
    }
    return 0;
}





你可能感兴趣的:(辛普森公式,体积计算,数值积分)