Description
A set of points S has the center of symmetry if there exists a point s (not necessarily in S) such that for every point p in S there exists a point q in S such that p-s = s-q.
Input
The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines contain two integer numbers each which are the x and y coordinates of a point. Every point is unique and we have that -10000000 <= x, y <= 10000000.
Output
For each set of input data print yes if the set of points has a center of symmetry and no otherwise.
Sample Input
1 8 1 10 3 6 6 8 6 2 3 -4 1 0 -2 -2 -2 4
Sample Output
yes
题意:给出一个 T 表示有 T 组输入数据,每个测试数据有一个 N,表示有 N 个点,每个点给出x和y,问这个图形是否关于一点对称。
通过排序,然后计算出每对点的中点,只要所有的点的中点都是一样就可以是yes。
#include <cstdio>
#include <algorithm>
using namespace std;
struct node
{
int x, y;
};
node point[10005];
int cmp(node a, node b)
{
if (a.x == b.x)
return a.y < b.y;
return a.x < b.x;
}
int main()
{
int T;
int n;
scanf("%d", &T);
while (T--)
{
scanf("%d", &n);
for (int i = 0; i < n; ++i)
scanf("%d%d", &point[i].x, &point[i].y);
sort(point, point + n, cmp);
int flag = 1;
int sym_x = point[0].x + point[n-1].x;
int sym_y = point[0].y + point[n-1].y;
for (int i = 1; i < n/2; ++i)
{
int j = n - i -1;
if (point[i].x + point[j].x != sym_x || point[i].y + point[j].y != sym_y)
{
flag = 0;
break;
}
}
if (flag == 1) printf("yes\n");
else printf("no\n");
}
return 0;
}