POJ---1791 Parallelogram Counting[数学题-平行四边形求个数]
Parallelogram Counting
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 5361 | Accepted: 1794 |
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
The first line of the input contains a single integer t (1 <= t <= 10), the number of test cases. It is followed by the input data for each test case.
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.
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
Output should contain t lines.
Line i contains an integer showing the number of the parallelograms as described above for test case i.
Line i contains an integer showing the number of the parallelograms as described above for test case i.
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
5 6
Source
【题目大意】:给出n个点,求出这n个点能够组成平行四边形的个数。
3)对于每一个k ,利用组合公式C(k,2)的答案就是平行四边行的个数
【解题思路】:
1)平行四边形的对角线的中点一定相交。<=> 如果有两条不同线段的中点相交,就是一个平行四边形
2)利用点坐标求出中点的集合,离散化后求出同个中点的出现的个数k。3)对于每一个k ,利用组合公式C(k,2)的答案就是平行四边行的个数
code:
1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 #define MAXN 1010 6 7 typedef struct point 8 { 9 int x,y; 10 }Point; 11 Point point[MAXN]; 12 Point mid[MAXN*MAXN]; 13 14 int cmp(const Point &a,const Point &b) 15 { 16 if(a.x==b.x) 17 return a.y<b.y; 18 return a.x<b.x; 19 } 20 21 int main() 22 { 23 int t; 24 int i,j; 25 int sum; 26 int n; 27 int cnt; 28 scanf("%d",&t); 29 while(t--) 30 { 31 scanf("%d",&n); 32 sum=0; 33 cnt=0; 34 for(i=0;i<n;i++) 35 scanf("%d%d",&point[i].x,&point[i].y); 36 for(i=0;i<n;i++) 37 for(j=i+1;j<n;j++) 38 { 39 mid[cnt].x=(point[i].x+point[j].x); 40 mid[cnt].y=(point[i].y+point[j].y); 41 cnt++; 42 } 43 sort(mid,mid+cnt,cmp); 44 int count=1; 45 for(i=0;i<cnt;i++) 46 { 47 if(mid[i].x==mid[i+1].x&&mid[i].y==mid[i+1].y) 48 count++; 49 else 50 { 51 sum+=(count-1)*count/2; 52 count=1; 53 } 54 } 55 printf("%d\n",sum); 56 } 57 return 0; 58 }
If you have any questions about this article, welcome to leave a message on the message board.
Brad(Bowen) Xu
E-Mail : maxxbw1992@gmail.com