Center of symmetry
Time Limit: 1000MS |
Memory Limit: 65536K |
|
Total Submissions: 2190 |
Accepted: 972 |
Description
Given is a set of n points with integer coordinates. Your task is to decide whether the set has a center of symmetry.
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
Source
Alberta Collegiate Programming Contest 2003.10.18
解题报告:这道题就行求能否找到一个中心点能使其他的每个点都有其对称点,其中中心点可以是题目所给的点,也可以假设一个中心点,把点的坐标排序即可!我求中点坐标的时候没有除以2,因为计算几何中能尽量不用除法,注意精度问题;
代码如下:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAX = 10005; int n, t; bool flag; struct Point { double x; double y; }p[MAX], center; int cmp(Point a, Point b)//先按x从大到小排序,若x相等时则按y从大到小排序 { if (a.x == b.x) return a.y > b.y; return a.x > b.x ; } bool Judge() { int i; if (n % 2) { center.x = 2 * p[n / 2].x; center.y = 2 * p[n / 2].y; } else { center.x = p[n / 2].x + p[n / 2 - 1].x; center.y = p[n / 2].y + p[n / 2 - 1].y; } for (i = 0; i < n / 2; ++i) { if (p[i].x + p[n - i - 1].x != center.x || p[i].y + p[n - i - 1].y != center.y) { return false; } } return true; } int main() { int i, j; scanf("%d", &t); while (t --) { scanf("%d", &n); for (i = 0; i < n; ++i) { scanf("%lf%lf", &p[i].x, &p[i].y); } sort(p, p + n, cmp); flag = Judge(); if (flag) { printf("yes\n"); } else { printf("no\n"); } } return 0; }