The 14th UESTC Programming Contest Final I - Intersection 计算几何、积分、精度

I - Intersection

Time Limit: 1000/1000MS (Java/Others)     Memory Limit: 262144/262144KB (Java/Others)
Submit  Status

Consider such a simple math problem:

There is a sphere and a right circular cone. The center of the sphere coincide with the apex of cone, and the height of the cone is equal to the 

radius of the sphere.

Could you tell me the volume of the intersection of cone and sphere?

Input

There is an integer  T  in the first line, indicates the number of test cases.

For each test, the only line contains two integers  r1  and  r2 , the radius of the sphere and the base radius of the cone.

1T10000   

1r1,r2100     

Output

For each test, output the answer in one line, in decimals, round to 4 decimal places.

Sample input and output

Sample Input Sample Output
1
1 2
1.1578

My Solution

画个图,然后,分成小圆锥和球用平面切下来一个帽 两块,小圆锥的半径可以用勾股定理和相似三角形来求,然后另外一部分用一元积分来求,

刚开始以为是二元积分,想了好长时间……

然后主要就是注意精度问题,如果有根号的,在最终等式中有平方或立方就应当先去掉俩根号。

有些题,如果碰到精度问题可以考虑 加一个eps 然后输出带要求位数小数的答案。 

#include<cstdio>
#include<bits/stdc++.h>

using namespace std;
//const int maxn=1e6+1000;
const double pi=acos(-1);

int main(void)
{
	int t;
	double r1,r2;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%lf%lf",&r1,&r2);
		double x1=r1*r1;
		double x2=r2*r2;
		double h=x1/sqrt(x1+x2);
		double v=pi*x1*x2/(x1+x2)*h*(1.0/3.0) + (2.0/3.0)*pi*r1*r1*r1-pi*x1*h+pi/3.0*h*x1*x1/(x1+x2);
		printf("%.4f\n",v);
		
	}
}

Thank you!



你可能感兴趣的:(ACM,计算几何,暨西南地区高校邀请赛,校赛Final)