POJ 1791 Parallelogram Counting(求平行四边形数量)
Description
There are n distinct points in the plane, given by their integer coordinates. Find the number of parallelograms whose vertices lie on these points. In other words, find the number of 4-element subsets of these points that can be written as {A, B, C, D} such that AB || CD, and BC || AD. No four points are in a straight line.
Input
Input starts with an integer T (≤ 15), denoting the number of test cases.
The first line of each test case contains an integer n (1 ≤ n ≤ 1000). Each of the next n lines, contains 2 space-separated integers x and y (the coordinates of a point) with magnitude (absolute value) of no more than 1000000000.
Output
For each case, print the case number and the number of parallelograms that can be formed.
Sample Input
2
6
0 0
2 0
4 0
1 1
3 1
5 1
7
-2 -1
8 9
5 7
1 1
4 8
2 0
9 8
Sample Output
Case 1: 5
Case 2: 6
给出点的坐标求出能连成平行四边形的数量。
思路:
用结构体记录每两个点的x坐标和与y坐标和( 相当于记录这两个点为对角线的情况 )然后循环判断多少个对角线的被交点平分(结构体中x,y的值相等)求出对角线的数量,然后C(sum,2)。
1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 int x[1010],y[1010]; 5 struct stu 6 { 7 int x,y; 8 }st[1010*1010/2]; 9 bool cmp(stu a,stu b) 10 { 11 if(a.x != b.x) 12 return a.x < b.x; 13 else 14 return a.y < b.y; 15 } 16 int main() 17 { 18 int t,ss=0; 19 scanf("%d",&t); 20 while(t--) 21 { 22 int n,i,j; 23 scanf("%d",&n); 24 for(i = 0 ; i < n ; i++) 25 { 26 scanf("%d %d",&x[i],&y[i]); 27 } 28 int k=0; 29 for(i = 0 ; i < n ; i++) 30 { 31 for(j = i+1 ; j < n ; j++) 32 { 33 st[k].x=x[i]+x[j]; 34 st[k++].y=y[i]+y[j]; 35 } 36 } 37 sort(st,st+k,cmp); 38 int num=0,sum=1,ans=0; 39 for(i = 1 ; i < k ; i++) 40 { 41 if(st[num].x == st[i].x && st[num].y == st[i].y) 42 sum++; //sum代表线的数量 43 else 44 { 45 ans+=(sum*(sum-1)/2); //这里是C(sum,2) 46 num=i; 47 sum=1; 48 49 } 50 } 51 printf("Case %d: %d\n",++ss,ans); 52 } 53 }