您的位置:首页 > 其它

acdream 1234-Two Cylinders(辛普森积分)

2017-10-19 13:45 176 查看


Two Cylinders

Special Judge Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB
(Java/Others)

Submit Statistic Next
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

题意:给你两个半径不同的圆柱体,计算垂直相交的体积

题解:当半径相同时,大一高数中学过,相交的体积为:16/3*R^3

当半径不相同时:



这里我们采用辛普森积分来进行积分(否则会越积越复杂)

                              


#include <math.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const double eps = 1e-10;
const double PI = acos(-1.0);
double r1,r2;
double f(double x)
{
return sqrt((r1*r1-x*x)*(r2*r2-x*x));
}
double simpson(double a,double b)
{
double c=a+(b-a)/2;
return (f(a)+4*f(c)+f(b))*(b-a)/6;
}
double asr(double a,double b) ///迭代求解数值积分
{
double mid=(a+b)/2.0;
double res=simpson(a,b);
if(fabs(res-simpson(a,mid)-simpson(mid,b))<eps)
{
return res;
}
else return asr(a,mid)+asr(mid,b);
}
int main(void)
{
while(scanf("%lf%lf",&r1,&r2)!=EOF)
{
if(r1==r2)
printf("%.4lf\n",16.0/3*r1*r1*r1);
else
{
if(r1>r2)
swap(r1,r2);
printf("%.4lf\n",8.0*asr(0,r1));
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: