熱門文章

2026年3月12日 星期四

ZeroJudge 解題筆記:c116. 00438 - The Circumference of the Circle

作者:王一哲
日期:2026年3月12日


ZeroJudge 題目連結:c116. 00438 - The Circumference of the Circle

解題想法


這題考數學。假設三角形 $ABC$,三個角的對邊長度分別為 $a, b, c$,三角形面積為 $$ area = \frac{1}{2}ab \sin C $$ 假設外接圓半徑為 $R$,由正弦定理可得 $$ \frac{a}{\sin A} = \frac{b}{\sin B} = \frac{c}{\sin C} = 2R ~\Rightarrow~ \sin C = \frac{c}{2R} $$ 再代回面積公式 $$ area = \frac{1}{2} ab \cdot \frac{c}{2R} = \frac{abc}{4R} ~\Rightarrow~ R = \frac{abc}{4 area} $$ 其中三角形面積可以用海龍公式求解 $$ area = \sqrt{s(s-a)(s-b)(s-c)} ~~~~~ s = \frac{a+b+c}{2} $$ 最後在計算圓周長 $2 \pi R$ 時,$\pi$ 要用題目給的 $3.141592653589793$。

Python 程式碼


使用時間約為 7 ms,記憶體約為 2.9 MB,通過測試。
import sys, math

for line in sys.stdin:
    x1, y1, x2, y2, x3, y3 = map(float, line.split())
    a = ((x1 - x2)**2 + (y1 - y2)**2)**0.5
    b = ((x2 - x3)**2 + (y2 - y3)**2)**0.5
    c = ((x3 - x1)**2 + (y3 - y1)**2)**0.5
    s = (a+b+c) / 2
    area = (s*(s-a)*(s-b)*(s-c))**0.5
    R = a*b*c/(4*area)
    print(f"{2*3.141592653589793*R:.2f}")


C++ 程式碼


使用時間約為 0 ms,記憶體約為 76 kB,通過測試。
#include <cstdio>
#include <cmath>

int main() {
    double x1, y1, x2, y2, x3, y3;
    while(scanf("%lf %lf %lf %lf %lf %lf", &x1, &y1, &x2, &y2, &x3, &y3) != EOF) {
        double a = sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
        double b = sqrt((x2 - x3)*(x2 - x3) + (y2 - y3)*(y2 - y3));
        double c = sqrt((x3 - x1)*(x3 - x1) + (y3 - y1)*(y3 - y1));
        double s = (a+b+c) / 2.0;
        double area = sqrt(s*(s-a)*(s-b)*(s-c));
        double R = a*b*c/(4.0*area);
        printf("%.2lf\n", 2*3.141592653589793*R);
    }
    return 0;
}


沒有留言:

張貼留言