ZOJ 3005 Electron Cloud

The electron is described in terms of its probability distribution or probability density. This probability distribution does not have definite cutoff points; its edges are somewhat fuzzy. Hence the term "electron cloud." This cloudy probability distribution takes on different shapes, depending on the state of the atom.

In order to describe a nuclear action, you are asked to tell where the electrons may occur.

Here we assume that the electrons only occur within a sphere whose location and radius are given. The action consists of only two atoms.

Input

There are multiple test cases. The first line of input is an integer T indicating the number of test cases. (0 < T ≤ 50)

Each test case consists of a line containing 8 integers X1, Y1, Z1, R1, X2, Y2, Z2 and R2, describing the coordinates and the radiuses of the two atoms. The absolute value of each coordinate do not exceed 100 and 0 < R1, R2 ≤ 100.

Output

For each test case, output the volume of space where the electrons may occur, accurated to the nearest 0.01.

Sample Input

3
0 0 0 1 0 0 2 1
0 0 0 2 0 0 2 2
0 0 0 2 0 0 2 1

Sample Output

8.38
56.55
36.00

其实就是求两个球的体积,其中包含三种情况 1、相离 2、相交 3、包含。这是一道zoj的3005,提交时WA,一直想不通错在哪?请大牛指点一下。

贴上我的代码:

#include"stdio.h" 
#include"math.h" 
const double pi=3.1415926; 
struct point{int x;int y;int z;}; 
double work(point a,point b) 
{ 
    return sqrt((double)(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z)); 
} 
int main() 
{ 
    int t; 
    scanf("%d",&t); 
    while(t--) 
    { 
        point a,b; 
        double r1,r2; 
        scanf("%d%d%d%lf%d%d%d%lf",&a.x,&a.y,&a.z,&r1,&b.x,&b.y,&b.z,&r2); 
        double r=work(a,b),v; 
        if(r1+r2-r<0.0000001) 
        {v=4.0/3.0*pi*(r1*r1*r1+r2*r2*r2); 
        printf("%.2lf\n",v);continue;
        } 
        if((r1-r)>0.0000001||(r2-r)>0.0000001) //此处判断错误,请参照评论
        { 
            if((r1-r2)<0.0000001) 
            { 
                v=4.0/3.0*pi*r2*r2*r2; 
                printf("%.2lf\n",v); 
                continue;
            } 
            else { 
                v=4.0/3.0*pi*r1*r1*r1; 
                printf("%.2lf\n",v); 
                continue;
            } 
        } 
        double v1,v2; 
        v=4.0/3.0*pi*(r1*r1*r1+r2*r2*r2); 
        double rab,ha,hb; 
        ha=((r1*r1-r2*r2)/r+r)/2.0; 
        hb=(r-(r1*r1-r2*r2)/r)/2.0; 
        rab=sqrt(r2*r2-hb*hb); 
        double temp=r2-hb; 
        hb=r1-ha; 
        ha=temp; 
        v1=pi*ha*(3*rab*rab+ha*ha)/6.0; 
        v2=pi*hb*(3*rab*rab+hb*hb)/6.0; 
        v=v-v1-v2; 
        printf("%.2lf\n",v);
   }
    } 
    return 0; 
}
posted @ 2011-05-01 21:07  Ac_smile  阅读(271)  评论(2编辑  收藏  举报