昨天的比赛我自己就出了这道题,但中间有许多煎熬,,因为一个PI的精度问题我WA了十次,后来同学说精度应该改成acos(-1),才过了。
Bahiyyah has a convex polygon with nn vertices P_0, P_1, \cdots , P_{n-1}P0,P1,⋯,Pn−1 in the counterclockwise order. Two vertices with consecutive indexes are adjacent, and besides, P_0P0 and P_{n-1}Pn−1 are adjacent. She also assigns a point Q inside the polygon which may appear on the border.
Now, Bahiyyah decides to roll the polygon along a straight line and calculate the length of the trajectory (or track) of point QQ.
To help clarify, we suppose P_n = P_0, P_{n+1} = P_1Pn=P0,Pn+1=P1 and assume the edge between P_0P0 and P_1P1 is lying on the line at first. At that point when the edge between P_{i-1}Pi−1 and P_iPi lies on the line, Bahiyyah rolls the polygon forward rotating the polygon along the vertex P_iPi until the next edge (which is between P_iPi and P_{i+1}Pi+1) meets the line. She will stop the rolling when the edge between P_nPn and P_{n+1}Pn+1 (which is same as the edge between P_0P0 and P_1P1) meets the line again.
Input
The input contains several test cases, and the first line is a positive integer TT indicating the number of test cases which is up to 5050.
For each test case, the first line contains an integer n (3 \le n \le 50)n(3≤n≤50) indicating the number of vertices of the given convex polygon. Following nn lines describe vertices of the polygon in the counterclockwise order. The i-th line of them contains two integers x_{i-1}xi−1 and y_{i-1}yi−1, which are the coordinates of point P_{i-1}Pi−1. The last line contains two integers x_QxQ and y_QyQ, which are the coordinates of point QQ.
We guarantee that all coordinates are in the range of -10^3−103 to 10^3103, and point QQ is located inside the polygon or lies on its border.
Output
For each test case, output a line containing Case #x: y, where xx is the test case number starting from 11, and yy is the length of the trajectory of the point QQ rounded to 33 places. We guarantee that 44-th place after the decimal point in the precise answer would not be 44 or 55.
输出时每行末尾的多余空格,不影响答案正确性
样例输入复制
4
4
0 0
2 0
2 2
0 2
1 1
3
0 0
2 1
1 2
1 1
5
0 0
1 0
2 2
1 3
-1 2
0 0
6
0 0
3 0
4 1
2 2
1 2
-1 1
1 0
样例输出复制
Case #1: 8.886
Case #2: 7.318
Case #3: 12.102
Case #4: 14.537
AC代码
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
#define PI acos(-1) //注意精度,在这里我WA了十次
struct polygon
{
double x;
double y;
};
int main()
{
int t;
scanf("%d",&t);
for(int i=1;i<=t;i++)
{
int n;
scanf("%d",&n);
polygon ver[55];
double len=0;
for(int j=1;j<=n;j++)
{
scanf("%lf%lf",&ver[j].x,&ver[j].y);
}
scanf("%lf%lf",&ver[0].x,&ver[0].y);
ver[n+1]=ver[1];//当滚动的边再一次是1时就停止
ver[n+2]=ver[2];
for(int k=1;k<=n;k++)
{
double a,b,c;
a=sqrt(pow(ver[k].x-ver[k+1].x,2)+pow(ver[k].y-ver[k+1].y,2));
b=sqrt(pow(ver[k+2].x-ver[k+1].x,2)+pow(ver[k+2].y-ver[k+1].y,2));
c=sqrt(pow(ver[k].x-ver[k+2].x,2)+pow(ver[k].y-ver[k+2].y,2));
double r=sqrt(pow(ver[0].x-ver[k+1].x,2)+pow(ver[0].y-ver[k+1].y,2));
double angle=PI-acos((a*a+b*b-c*c)/(2*b*a)); //角余弦公式,PI减去滚动角
len+=(angle*r); //弧长=角度*半径
}
printf("Case #%d: %.3lf\n",i,len);
}
return 0;
}