acdream 1234 Two Cylinders

Two Cylinders

Special Judge Time 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 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
Source
Andrew Stankevich Contest 3
Manager


题解及代码:

        这道题的意思很简单,就是求两个垂直相交的圆柱的重合体积。推导的思想可以见:点击打开链接

        根据上面的方法我们可以推出截面公式是sqrt(R*R-x*x)*sqrt(r*r-x*x),然后积分的上下限是0--r。

        虽然这公式推出来了,但是积分很困难(没推出来= =!),下面的代码用的是辛普森积分法,学习了一下,挺简单的。

        主要就是这个:

        这个公式在数比较小的时候,误差不大,但是数比较大的时候,误差就很大了,所以计算过程中要使用二分来把区间减小,减小误差。


#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const double eps=1e-7;

double R,r;

double f(double n)
{
    return 8*sqrt(R*R-n*n)*sqrt(r*r-n*n);
}

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

double cal(double a,double b)
{
    double sum=simpson(a,b),mid=(a+b)/2.0;
    double t=simpson(a,mid)+simpson(mid,b);

    if(fabs(t-sum)<eps) return sum;
    return cal(a,mid)+cal(mid,b);
}
int main()
{
    scanf("%lf%lf",&R,&r);
    if(R<r) swap(R,r);

    printf("%.5lf\n",cal(0,r));
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值