Loop of Chocolate

题目描述

Let’s make sweets of a fancy shape that is a loop of chocolate.

Figure A.1. A loop of chocolate formed by a union of six spheres

The shape of a loop is formed by a union of a number of spheres of the same size, where every sphere intersects with exactly two others.

(a) Union of four spheres (b) Four intersections of the four spheres in (a)

Figure A.2. A loop of chocolate formed by a union of four spheres

Your job is to write a program that, for given the size and the positions of spheres, computes the total volume of the union of the spheres, i.e., the amount of chocolate required for filling the loop formed by the union.

[Hints] Two spheres of the same radius r intersect each other when the distance between their centers, d, is less than 2r. The volume of the intersection is known to be

The volume of the sphere of radius r is 4πr3/3.

输入

The input consists of a single test case of the following format.
n r
x1 y1 z1
.
.
.
xn yn zn
n and r are integers. n is the number of spheres (4 ≤ n ≤ 100). All the spheres has the same radius r (2 ≤ r ≤ 100). (xk, yk, zk) indicates the coordinates of the center of the k-th sphere (k = 1, . . . , n). All of xk, yk, and zk are integers between −100 and 100, inclusive.
The k-th and the k + 1-th spheres intersect each other for 1 ≤ k < n. The 1-th and the n-th spheres also intersect. No other pairs of spheres intersect.

输出

Output in a line the volume of the union of the spheres. Relative error of the output should be within 10−7.

样例输入 

【样例1】
6 9
20 0 10
20 10 0
10 20 0
0 20 10
0 10 20
10 0 20
【样例2】
4 12
10 10 0
10 -10 0
-10 -10 0
-10 10 0
【样例3】
6 9
23 3 13
20 10 0
10 20 0
3 23 13
0 10 20
10 0 20
【样例4】
4 2
0 0 0
3 0 0
3 3 0
0 3 0
【样例5】
8 70
100 100 0
0 100 0
-100 100 0
-100 0 0
-100 -100 0
0 -100 0
100 -100 0
100 0 0

样例输出 

【样例1】
17149.528141
【样例2】
27813.56696
【样例3】
17470.837758
【样例4】
122.52211349
【样例5】
10220648.1

根据题意来即可

#include <iostream>
#pragma warning (disable:4996)
#include <cmath>
using namespace std;
const double PI = acos(-1);
int n;
double r;
double x[110], y[110], z[110];
int main() {
    scanf("%d %lf", &n, &r);
    for (int i = 1; i <= n; ++i) {
        scanf("%lf %lf %lf", &x[i], &y[i], &z[i]);
    }
    double ans = n * 4 * PI * r * r * r / 3;
    for (int i = 1; i < n; ++i) {
        double len = sqrt(pow(x[i] - x[i + 1], 2) + pow(y[i] - y[i + 1], 2) + pow(z[i] - z[i + 1], 2));
        double v = 2.0 / 3 * PI * pow(r - len / 2, 2) * (2 * r + len / 2);
        ans -= v;
    }
    double len = sqrt(pow(x[1] - x[n], 2) + pow(y[1] - y[n], 2) + pow(z[1] - z[n], 2));
    double v= 2.0 / 3 * PI * pow(r - len / 2, 2) * (2 * r + len / 2);
    ans -= v;
    printf("%.8lf", ans);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值