半徑 R=1 的圓形蛋糕,一刀切成兩部分,面積比 a:b,求切口弦長。多組輸入,N=0 結束。
S₁ * b = S₂ * a。double area(double theta) { return (theta - sin(theta)) / 2.0; }
double chord(double a, double b) {
double lo = 0, hi = acos(-1.0); // π
for (int iter = 0; iter < 60; iter++) { // 雙精度足夠
double mid = (lo + hi) / 2;
double s1 = area(mid);
double s2 = acos(-1.0) - s1;
if (s1 * b <= s2 * a) lo = mid;
else hi = mid;
}
return 2 * sin(lo / 2);
}
int main() {
double a, b;
while (scanf("%lf %lf", &a, &b) == 2) {
if (a > b) { double t=a; a=b; b=t; } // 保證 a<=b
printf("%.4f\n", chord(a, b));
}
return 0;
}
fabs(hi-lo) > 1e-12 作條件。%.4f。