Algorithm:
1. enumerate any two vertices and determine whether the coordinates of the other two points exist based on the formula.
2. Search Algorithm. You can use hash or binary search.
View code
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<algorithm>#include<iostream>using namespace std;struct Point{ int x,y; bool operator < ( const Point &A) const { if( x != A.x ) return x < A.x; return y < A.y; }}tmp[1010];struct node{ Point p; int next;}nt[201000];int head[201000],size;int find(Point A){ int key = abs(A.x+A.y); for( int e = head[key]; e != -1; e = nt[e].next) { if( nt[e].p.x == A.x && nt[e].p.y == A.y ) return 1; } return 0; }int main( ){ int N; while( scanf("%d", &N), N) { memset(head, -1,sizeof(head)); size = 0; for( int i = 1; i <= N; i++) { scanf("%d%d",&tmp[i].x, &tmp[i].y); int key = abs(tmp[i].x+tmp[i].y); nt[size].p = tmp[i]; nt[size].next = head[key]; head[key] = size++; } sort( tmp + 1, tmp + N + 1); int num = 0; for( int i = 1; i <= N; i++) { for( int j = i + 1; j <= N; j++) { int a1 = tmp[i].x; int a2 = tmp[i].y; int b1 = tmp[j].x; int b2 = tmp[j].y; Point A,B; A.x = a1 + a2 - b2; A.y = a2 + b1 - a1; B.x = b1 + a2 - b2; B.y = b2 + b1 - a1; if( find(A) && find(B) ) num++; } } printf("%d\n",num / 2); } }