Two Cylinders (辛普森公式处理积分)
2016-06-26 18:23
357 查看
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
Sample Output
题目大意:
此题要计算两个圆柱体垂直相交之后形成的体积,且两个圆柱体的半径不一样。
题目分析:
可以先依据两个半径相等的圆柱体相交所得物体(称为:牟合方盖)的体积计算方法来推导半径不相等的物体的体积,计算牟合方盖的体积时,我们都是利用定积分,先计算在第一卦限中的体积,最后乘以8就行了,具体过程如下。
求解两柱体x2+y2=R2和x2+z2=R2相交部分的体积。
而此题所要求的是柱体x2+y2=R22和x2+z2=R12相交的体积。
我们同样也是只算第一卦限的体积,最后乘以8即可。
代码实现如下:
#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;
}
题面描述:
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相交部分的体积。
而此题所要求的是柱体x2+y2=R22和x2+z2=R12相交的体积。
我们同样也是只算第一卦限的体积,最后乘以8即可。
代码实现如下:
#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;
}
相关文章推荐
- 子进程的创建
- JAVAweb开发技术-------(三)JSP
- 手机APP兼容性测试
- java.lang.IllegalStateException: commit already called
- 练习Android大图轮播效果的实现-大致布局
- [代码实例][gtk+]创建按钮
- 个人独立游戏总结
- 第九届河南省赛 A nyoj1272 表达式求值
- 《OD学hadoop》第一周0625 LINUX作业一:Linux系统基本命令(一)
- 数据结构复习之二叉树(2)
- 单链表的所有操作list
- 动态规划-装配线调度问题
- 刘秀的东汉
- Java中数组和集合的转换
- consul简介
- 内网主机,智能设备的互联网访问方案,可联网即可当服务器。
- 面向对象三大基本特性,五大基本原则
- JTAG无法识别单片机(JTAG Device chain)
- 中国龙
- excel批量分类替换方法