题目:给定n个点,问是否存在一条垂直的对称轴
如果存在的话,那么必定是平分最右和最左的点。那么对称轴的方程可以写出来。输入的时候,可以坐标都乘以2来排除对称轴是小数的情况。
然后枚举点还判断即可。可以用个set来保存点。
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define inf (0x3f3f3f3f) typedef long long int LL; #include <iostream> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <string> const double eps = 1e-7; bool same (double a,double b) { return fabs(a-b)<eps; } struct coor { int x,y; coor(){} coor(int xx,int yy):x(xx),y(yy){} double operator ^(coor rhs) const //计算叉积(向量积),返回数值即可 { return x*rhs.y - y*rhs.x; } coor operator -(coor rhs) const //坐标相减,a-b得到向量ba,返回一个向量(坐标形式) { return coor(x-rhs.x,y-rhs.y); } double operator *(coor rhs) const //数量积,返回数值即可 { return x*rhs.x + y*rhs.y; } bool operator ==(coor rhs) const { return same(x,rhs.x)&&same(y,rhs.y); //same的定义其实就是和eps比较 } bool operator < (coor rhs) const { if (x!=rhs.x) return x<rhs.x; else return y<rhs.y; } }; //记得这里有个分号 set<struct coor>ss; void work () { ss.clear(); int n; cin>>n; for (int i=1;i<=n;++i) { int x,y; scanf("%d%d",&x,&y); x*=2; y*=2; ss.insert(coor(x,y)); } int bx; set<struct coor>::iterator it = ss.begin(); //printf ("%d %d ",it->x,it->y); bx = it->x; while (it!=ss.end()) ++it; it--; bx += it->x; bx/=2; int now_calc = 0; for (it=ss.begin();it!=ss.end() && 2*now_calc <= n;it++) { int tx = 2*bx - it->x; int ty = it->y; if (ss.find(coor(tx,ty))==ss.end()) { printf ("NO "); return ; } now_calc++; } printf ("YES "); return ; } int main() { #ifdef local freopen("data.txt","r",stdin); #endif int t; cin>>t; while (t--) work(); return 0; }