在结构体中排序 #include<iostream> #include<algorithm> #include<cmath> #define MAX 1111 using namespace std; typedef struct point { int x,y; bool operator < (const point &tt) const { if(x != tt.x) return x < tt.x;//在结构体中排序 return y < tt.y; } }Point; point p[MAX]; int n; /*bool cmp(const point &a, const point &b) { if(a.x==b.x) return a.y<b.y; return a.x<b.x; }*/ int search(point q) { int l=0,r=n-1,m,i,j,k=0; while(l<=r) { m=(l+r)/2; if(q.x==p[m].x) { for(i=m;p[i].x==q.x&&i<n;i++) //往右找 if(p[i].y==q.y) return 1; for(i=m;p[i].x==q.x&&i>=0;i--) //往左找 if(p[i].y==q.y) return 1; return 0; //如果左右都没有找到 就必须返回一个0 否则进入死循环 } else if(q.x>p[m].x) l=m+1; else r=m-1; } return 0; } int main() { int i,j,k; point c,d; while(cin>>n&&n) { int ans=0; for(i=0;i<n;i++) cin>>p[i].x>>p[i].y; sort(p,p+n); for(i=0;i<n;i++) for(j=i+1;j<n;j++) { c.x=p[j%n].x+p[i].y-p[j%n].y; c.y=p[j%n].y+p[j%n].x-p[i].x; d.x=p[i].x+p[i].y-p[j%n].y; d.y=p[i].y+p[j%n].x-p[i].x; if(search(c)==1&&search(d)==1) ans++; } cout<<ans/2<<endl; } return 0; } 在sort中排序 #include<iostream> #include<algorithm> #include<cmath> #define MAX 1111 using namespace std; typedef struct point { int x,y; /* bool operator < (const point &tt) const { if(x != tt.x) return x < tt.x;//在结构体中排序 return y < tt.y; }*/ }Point; point p[MAX]; int n; bool cmp(const point &a, const point &b) { if(a.x==b.x) return a.y<b.y; return a.x<b.x; } int search(point q) { int l=0,r=n-1,m,i,j,k=0; while(l<=r) { m=(l+r)/2; if(q.x==p[m].x) { for(i=m;p[i].x==q.x&&i<n;i++) //往右找 if(p[i].y==q.y) return 1; for(i=m;p[i].x==q.x&&i>=0;i--) //往左找 if(p[i].y==q.y) return 1; return 0; //如果左右都没有找到 就必须返回一个0 否则进入死循环 } else if(q.x>p[m].x) l=m+1; else r=m-1; } return 0; } int main() { int i,j,k; point c,d; while(cin>>n&&n) { int ans=0; for(i=0;i<n;i++) cin>>p[i].x>>p[i].y; sort(p,p+n); for(i=0;i<n;i++) for(j=i+1;j<n;j++) { c.x=p[j%n].x+p[i].y-p[j%n].y; c.y=p[j%n].y+p[j%n].x-p[i].x; d.x=p[i].x+p[i].y-p[j%n].y; d.y=p[i].y+p[j%n].x-p[i].x; if(search(c)==1&&search(d)==1) ans++; } cout<<ans/2<<endl;重复找了一倍 } return 0; } http://acm.pku.edu.cn/JudgeOnline/problem?id=2002