SZTUOJ 1121 - The Area of a Sector
SZTUOJ 1013 - The Area of a Sector
Description
Given a circle and two points on it, calculate the area of the sector with its central angle no more than 180 degrees.
Input
There are multiple test cases.
Each line contains 6 float numbers denote the center of the circle xc, yc, the two points on the circle x1, y1 and x2, y2.
1 <= xc, yc, x1, y1, x2, y2 <= 10000.
Output
The area of the sector. The result should be accurated to three decimal places.
Sample Input
0 0 1 1 -1 1
2.5 2.5 3.25 3.5 3.5 3.25
Sample Output
1.571
0.222
Hint
With math.h, you could define pi (3.1415926...) as const double pi = acos(-1.0);
Inverse trigonometric function could be obtained by acos, atan, asin, etc..
Be careful that angles like pi in C/C++ are in 3.1415926... type rather than 180.
You’d better use double instead of float. Load double with "%lf" but output it with "%f".
Source
2021 SZTU AI Class Qualifying.
思路分析
這道題來自2021倫琴AI班選拔機試C題,當時只有包括我在內的兩個人做了出來。但題目本身並不難,根據描述按步編寫即可。
本題需要求解扇形面積。根據圓的面積公式,我們可以推匯出半徑為\(r\),圓心角為\(\theta\)的扇形面積公式:
\[S_\theta=\frac{\theta}{2}r^2\ (0<\theta\leq2\pi) \]根據上述公式,我們瞭解到,若想求解扇形面積,需要先求出扇形半徑\(r\)和圓心角\(\theta\).
根據輸入資料\(O(x_0,y_0),P_1(x_1,y_1),P_2(x_2,y_2)\)
作\(\triangle OP_1P_2\)的中垂線,根據幾何性質,有:
\[\sin{\frac{1}{2}\angle O}=\sin{\frac{\theta}{2}}=\frac{l}{2r} \]即:
\[\theta=2\arcsin{\frac{l}{2r}} \]至此,求解面積所需的全部參量已經求出,帶入扇形面積公式即可得解。
程式碼實現
// Language: C
#include<stdio.h>
#include<math.h>
typedef struct Point{
double x;
double y;
}point;
int main() {
point o, p1, p2;
double ox, oy, x1, y1, x2, y2;
while (scanf("%lf%lf%lf%lf%lf%lf", &o.x, &o.y, &p1.x, &p1.y, &p2.x, &p2.y) != EOF) {
double r = hypot(p1.x - o.x, p1.y - o.y);
double l = hypot(p1.x - p2.x, p1.y - p2.y);
double theta = 2 * asin(0.5 * l / r);
printf("%.3f\n", r * r * theta / 2);
}
return 0;
}