zoukankan      html  css  js  c++  java
  • ZJU-ICPC Summer 2020 Contest 9 B

    B. Ball
    In Geometry, a spherical cap is a portion of a sphere cut off by a plane. If the radius of the sphere is (r) and the height of the spherical cap is (h), the volume of the spherical cap is (πh^{2}(r−frac{1}{3}h)).

    There are two spheres in the three-dimensional space whose radius are (r_1) and (r_2), center coordinates are ((x1,y1,z1)) and ((x2,y2,z2)). Your task is to calculate the volume of their combination.

    Input
    The first line contains four integers, denoting (x1, y1, z1, r1(0 leq x1,y1,z1,r1leq 100)).

    The second line contains four integers, denoting (x2, y2, z2, r2(0 leq x2,y2,z2,r2 leq 100)).

    Output
    Output one line with one real number, the volume of their combination.

    Your answer will be consider correct if its absolute or relative error will not exceed (10^{−6}).

    Examples

    inputCopy
    0 0 0 4
    3 4 0 3
    outputCopy
    361.911473693544167

    inputCopy
    0 0 0 5
    3 0 0 3
    outputCopy
    538.521340702850352

    求两个球体并起来的体积。

    首先分三种情况:包含,分离,相交。

    前两种利用球体公式,后一种用题目给的球冠公式做。

    
    #include<bits/stdc++.h>
    using namespace std;
    
    const long double pi = acos(-1);
    
    long double getans(long double r,long double h){
        return pi * h * h * (3 * r - h);
    }
    long double get(long double r){
        return 4 * pi * r * r * r;
    }
    
    long double ax,ay,az,bx,by,bz,ar,br;
    
    int main(){
        cin >> ax >> ay >> az >> ar;
        cin >> bx >> by >> bz >> br;
        
        long double dis = (ax - bx) * (ax - bx) + (ay - by) * (ay - by) + (az - bz) * (az - bz);
        
        if(dis >= (ar + br) * (ar + br)){
            printf("%.15Lf
    ",(get(ar) + get(br)) / 3.0);
            return 0;
        }
        
        dis = sqrt(dis);
        
        if(dis + ar <= br || dis + br <= ar){
            printf("%.15Lf
    ",get(max(ar,br))/3.0);
            return 0;
        }
        
        long double x = ar * ar - br * br + dis * dis; x = x / (2 * dis); 
        long double y = dis - x;
        
        printf("%.15Lf
    ",(getans(ar,ar + x) + getans(br,br + y)) / 3.0);
        return 0;
    }
    
    
    
  • 相关阅读:
    关于嵌套循环的循环初始化语句问题:
    自己写的小程序
    计算1-1/3+1/5-1/7+···的前n项和
    终于弄好了 homework-09
    现代C++作业2 与 围棋homework-06
    C++11 能好怎?
    黄金点游戏之客户端(homework-05)
    惊艳的随机化方法 -World Search (homework-04)
    GUI、模块化与结对编程(homework-03)
    最大二位子数组和问题(homework-02)
  • 原文地址:https://www.cnblogs.com/zzhzzh123/p/13385917.html
Copyright © 2011-2022 走看看