Submit Status
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
//题意:输入一个n,再输入n个点的坐标。
给你n个点的坐标让你求出这n个点可以组成几个平行四边形。
//思路:
因为平行四边形的两条对角线的交点是唯一的,所以先求出这n个点所能组成的所有线段的中点(n*(n-1)/2个中点),对其进行排序后,对这些中点进行计算,如果在一个中点处有num条线段相交,那么在这个中点处可以组成num*(num-1)/2个平行四边形。最所有中点模拟一遍对其求和即为所得。
AC代码:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct node { int x,y; }; node point[1010]; node mid[1010*1010]; int cmp(node A,node B) { if(A.x==B.x) return A.y<B.y; return A.x<B.x; } int main() { int t; int i,j; int sum; int n; int cnt;int k=0; scanf("%d",&t); while(t--) { scanf("%d",&n); sum=0; cnt=0; for(i=0;i<n;i++) scanf("%d%d",&point[i].x,&point[i].y); for(i=0;i<n;i++) for(j=i+1;j<n;j++) { mid[cnt].x=(point[i].x+point[j].x); mid[cnt].y=(point[i].y+point[j].y); cnt++; } sort(mid,mid+cnt,cmp); //中点排序//对于一个平行四边形,其两条对角线的的交点是其中点。因此可以计算每条线段的中点是否相等判断是否相等 int count=1; for(i=0;i<cnt;i++) { if(mid[i].x==mid[i+1].x&&mid[i].y==mid[i+1].y) count++; else { sum+=(count-1)*count/2; count=1; } } printf("Case %d: ",++k); printf("%d\n",sum); } return 0; }