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; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!