UVa 358 - Don't Have A Cow

contents

  1. 1. Problem
  2. 2. Sample input
  3. 3. Sample output
  4. 4. Solution

Problem

有一個半徑為 R 的圓草地,接著將一頭牛綁在圓周上 (定點),請問繩長多少的時候,恰好能夠將圓佔有 P% 的面積。

Sample input

1
2
1
100 0.33

Sample output

1
R = 100, P = 0.33, Rope = 13.24

Solution

將圓放在坐標軸上,假設綁定的位置於 (R, 0),而接著二分另外一點 (於圓上) 可以拉到綁定的位置。

計算繩長和面積即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;
#define eps 1e-8
int main() {
int testcase;
double R, P;
const double pi = acos(-1);
scanf("%d", &testcase);
while (testcase--) {
scanf("%lf %lf", &R, &P);
double l = 0, r = pi, mid, ret = 0;
double A = R * R * pi;
while (fabs(l - r) > eps) {
mid = (l + r)/2;
ret = hypot(R * cos(mid) - R, R * sin(mid));
double theta = (pi - mid)/2;
double area = (ret * ret * theta/2 + (R * R * mid/2 - R * R * sin(mid)/2))*2;
if (area < A * P)
l = mid;
else
r = mid;
}
printf("R = %.0lf, P = %.2lf, Rope = %.2lf\n", R, P, ret);
if(testcase)
puts("");
}
return 0;
}
/*
*/