码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5060 War

时间:2014-10-06 12:29:10      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:simpson积分法

War

Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 98    Accepted Submission(s): 28
Special Judge


Problem Description
Long long ago there are two countrys in the universe. Each country haves its own manor in 3-dimension space. Country A‘s manor occupys x^2+y^2+z^2<=R^2. Country B‘s manor occupys x^2+y^2<=HR^2 && |z|<=HZ. There may be a war between them. The occurrence of a war have a certain probability. 
We calculate the probability as follow steps.
1. VC=volume of insection manor of A and B.
2. VU=volume of union manor of A and B.
3. probability=VC/VU
 

Input
Multi test cases(about 1000000). Each case contain one line. The first line contains three integers R,HR,HZ. Process to end of file.

[Technical Specification]
0< R,HR,HZ<=100
 

Output
For each case,output the probability of the war which happens between A and B. The answer should accurate to six decimal places.
 

Sample Input
1 1 1 2 1 1
 

Sample Output
0.666667 0.187500
 

Source
 


题解及代码:


       这道题的意思很简单:给定中心重合的一个球和一个圆柱,求出其重合体积占所有体积的比例。

      这题写起来很麻烦,因为要分成5类分别写(可耻de把官方的图扣下来 = =!)

bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣

分类大致就是分成这5类,积分的方式这里使用的是simpson积分法,只要知道被积函数和上下限就可以了,不用自己做不定积分。


#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const double pi=3.14159265358979,eps=1e-7;
double r,hr,hz;

double f(double n)
{
    return pi*(r*r-n*n);
}

double simpson(double a,double b)
{
    return (b-a)/6.0*(f(a)+4*f((a+b)/2.0)+f(b));
}

double cal(double a,double b)
{
    double sum=simpson(a,b),mid=(a+b)/2.0;
    double t=simpson(a,mid)+simpson(mid,b);

    if(fabs(t-sum)<eps) return sum;

    return cal(a,mid)+cal(mid,b);
}


int main()
{

    while(scanf("%lf%lf%lf",&r,&hr,&hz)!=EOF)
    {
        double v=0,hv=0;
        if(hr>=r&&hz>=r)
        {
            v=4.0/3.0*pi*r*r*r;
            hv=2*pi*hr*hr*hz;
            printf("%.6lf\n",v/hv);
            continue;
        }
        if(hr>=r&&hz<r)
        {
            v=4.0/3.0*pi*r*r*r;
            double t=2*cal(hz,r);
            hv=2*pi*hr*hr*hz;
            printf("%.6lf\n",(v-t)/(hv+t));
            continue;
        }
        if(r*r>=hr*hr+hz*hz)
        {
            v=4.0/3.0*pi*r*r*r;
            hv=2*pi*hr*hr*hz;
            printf("%.6lf\n",hv/v);
            continue;
        }
        if(hr<r&&hz>=r)
        {
            v=4.0/3.0*pi*r*r*r;
            double t=2*cal(sqrt(r*r-hr*hr),r)+2*sqrt(r*r-hr*hr)*pi*hr*hr;
            hv=2*pi*hr*hr*hz;
            printf("%.6lf\n",t/(hv+v-t));
            continue;
        }
        v=4.0/3.0*pi*r*r*r;
        hv=2*pi*hr*hr*hz;
        double t=2*cal(sqrt(r*r-hr*hr),hz)+2*sqrt(r*r-hr*hr)*pi*hr*hr;
        printf("%.6lf\n",t/(hv+v-t));
    }
    return 0;
}






hdu 5060 War

标签:simpson积分法

原文地址:http://blog.csdn.net/knight_kaka/article/details/39826979

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!