POJ---1791 Parallelogram Counting[数学题-平行四边形求个数]
2012-08-23 00:07
281 查看
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
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.
Output
Output should contain t lines.
Line i contains an integer showing the number of the parallelograms as described above for test case i.
Sample Input
Sample Output
Source
Tehran Sharif 2004 Preliminary
【题目大意】:给出n个点,求出这n个点能够组成平行四边形的个数。
【解题思路】:
1)平行四边形的对角线的中点一定相交。<=> 如果有两条不同线段的中点相交,就是一个平行四边形
2)利用点坐标求出中点的集合,离散化后求出同个中点的出现的个数k。
3)对于每一个k ,利用组合公式C(k,2)的答案就是平行四边行的个数
code:
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 5361 | Accepted: 1794 |
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.
Output
Output should contain t lines.
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
Tehran Sharif 2004 Preliminary
【题目大意】:给出n个点,求出这n个点能够组成平行四边形的个数。
【解题思路】:
1)平行四边形的对角线的中点一定相交。<=> 如果有两条不同线段的中点相交,就是一个平行四边形
2)利用点坐标求出中点的集合,离散化后求出同个中点的出现的个数k。
3)对于每一个k ,利用组合公式C(k,2)的答案就是平行四边行的个数
code:
#include<iostream> #include<algorithm> using namespace std; #define MAXN 1010 typedef struct point { int x,y; }Point; Point point[MAXN]; Point mid[MAXN*MAXN]; int cmp(const Point &a,const Point &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; 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("%d\n",sum); } return 0; }
相关文章推荐
- POJ 2624 算平行四边形的第四个点
- [从头学数学] 第143节 平行四边形 小结与复习题
- poj 2624 求平行四边形的第四个点坐标
- light oj 1305 - Area of a Parallelogram (数学 平行四边形坐标和面积)
- [从头学数学] 第60节 平行四边形和梯形
- 如何用数学课件制作工具推导平行四边形的面积公式
- LightOJ 1305 (数学求平行四边形面积)
- 平行四边形数 FZU 2231 简单的计算数学
- 数学 求平行四边形的面积和坐标
- POJ 1160 区间DP + 平行四边形优化
- POJ 3100 & ZOJ 2818 & HDU 2740 Root of the Problem(数学)
- POJ 2906 数学期望
- [Java基础]打印平行四边形
- POJ 1207 求最大数链长度 暴力枚举数学题
- POJ - 1850 Code(组合数学推公式)
- (one day one problem)poj 2800 Joseph's Problem (数学)
- 数学问题--POJ 2242
- 平行x 轴的线段 是否 遮掩 计算几何的扩大数据运算的典型应用,扩大根号2倍之后就避免了小数。 poj 3347 Kadj Squares
- POJ 1850 Code/POJ 1496 Word Index(组合数学-字母串序号)
- POJ1942 Paths on a Grid (组合数学)