首页 > 技术文章 > hdu 5060 五种情况求圆柱体与球体交

zibaohun 2014-10-13 21:35 原文

http://acm.hdu.edu.cn/showproblem.php?pid=5060

官方题解http://bestcoder.hdu.edu.cn/给复杂了

实际上用圆柱体与球体体积差的积分公式(pi*r*r - pi*x*x)即可轻松解决五种情况

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <set>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define clr0(x) memset(x,0,sizeof(x))
typedef long long LL;

const double pi = acos ( -1.0 ) ;

int R , HR , HZ ;


double f2 ( double x1 , double x2 ) {
	return R * R * pi * ( x2 - x1 ) - 1.0 / 3.0 * pi * ( x2 * x2 * x2 - x1 * x1 * x1 ) ;
}

int solve () {
	double V1 = pi * R * R * R * 4.0 / 3.0 ;
	double V2 = 2.0 * HR * HR * pi * HZ ;
	if ( HR * HR + HZ * HZ <= R * R ) {
		printf ( "%.6f\n" , V2 / V1 ) ;
	} else if ( HR >= R && HZ >= R ) {
		printf ( "%.6f\n" , V1 / V2 ) ;
	} else {
	    double V;
	    if ( HR <= R && HZ <= R ) {
            double y1 = HZ ;
            double y2 = sqrt ( R * R - HR * HR ) ;
            V = 2.0 * ( HR * HR * y2 * pi + f2 ( y2 , R ) - f2 ( y1 , R ) ) ;
        } else if ( HR > R && HZ <= R ) {
            double y1 = HZ ;
            double y2 = 0 ;
            V = 2.0 * ( f2 ( y2 , R ) - f2 ( y1 , R ) ) ;
        } else if ( HR <= R && HZ >= R ) {
            double y = sqrt ( R * R - HR * HR ) ;
            V = 2.0 * ( HR * HR * y * pi + f2 ( y , R ) ) ;
        }
        printf ( "%.6f\n" , V / ( V1 + V2 - V ) ) ;
	}
}

int main () {
	while ( ~RD3(R , HR , HZ ) )
        solve () ;
	return 0 ;
}


推荐阅读