Problem B. 

GSS and Interesting SculptureInput file: standard inputOutput file: standard outputTime limit: 1 secondsMemory limit: 512 mebibytesGSS is painting an strange sculpture, the sculpture contests of two balls and they may intersect. Your task is tocalculate the area of the surface of the sculpture.InputInput contains multiple cases, please process to the end of input.For each line, there are three integers, R, r, L, R and r the radius of the two balls, L is the distance of the centerof two balls.0 < R, r, L ≤ 100, |R − r| < L ≤ |R + r|OutputFor each input, output one line with the answer, the area of the surface of the sculpture. Let the standard answerbe a, and your answer be b, your answer will be considered as correct if and only if |a−b|max(a,1) < 10−6.Examples

standard input                   standard output

         3 4 5                          271.433605270158

         3 3 3                          169.646003293849

         1 2 3                           62.831853071796

题意:两个球相交 , 已知 r , R , L . L为两圆的圆心距 ,求两圆相交后组成的物体的表面积。

思路:S = 圆1的表面积 + 圆2的表面积 - 圆1相交部分的球冠表面积 - 圆2相交部分的球冠表面积

在这里引用球冠表面积的求法:

若球半径是R,球冠的高是h,球冠面积是S,则S=2πRh,若球冠的底的半径是r,则S=π(r^2+h^2)。


利用微积分原理推导公式

上式中可以推出:x = (r^2 - R^2 + L^2)/2*L ;

h1 = r-x;

h2 = R-L-x;

 将h1 , h2 带入公式即可得;

下面代码:

#include<stdio.h>
const double pi = 3.141592653589793238;
int main()
{
	double R,r,L;
	while(~scanf("%lf %lf %lf",&r, &R, &L))
	{
	if(r +R > L)
	{
		double x = (r * r - R*R + L*L) / (2.0 * L);
		double s1 = 4 * pi *r *r;
		double s2 = 4 * pi * R *R;
		double S = s1+ s2;
		double h1 = r - x;
		double h2 =R-(L -x);
		double res = S - 2 * pi *r*h1 - 2*pi*R*h2;
		printf("%.12lf\n",res);
	}
	else
	{
		double s1 = 4 * pi *r *r;
		double s2 = 4 * pi * R *R;
		double S = s1+ s2;
		printf("%.12lf\n",S);
	}

}
	
	
	return 0;
}