计算数学-题目点击打开链接
Problem Description
在一个平面内给定n个点,任意三个点不在同一条直线上,用这些点可以构成多少个平行四边形?一个点可以同时属于多个平行四边形。
Input
多组数据(<=10),处理到EOF。
每组数据第一行一个整数n(4<=n<=500)。接下来n行每行两个整数xi,yi(0<=xi,yi<=1e9),表示每个点的坐标。
Output
每组数据输出一个整数,表示用这些点能构成多少个平行四边形。
Sample Input
4
0 1
1 0
1 1
2 0
Sample Output
1
Source
福州大学第十三届程序设计竞赛
分析:
利用四边形的中心用对角两点来求,每两个重复的中心构成一个四边形(任意3点不在一条直线),求出任意两个点的中点,因为题目中已经表明任意三个点不在一条直线上,所以中点相同的点中任选两个都能组成平行四边形。
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct point { int x,y; }point[500]; struct node { int zx,zy; }a[250000]; bool cmp(node p,node q) { if(p.zx==q.zx) { return p.zy<q.zy; } else return p.zx<q.zx; } int main() { int n; while(~scanf("%d",&n)) { for(int i=0;i<n;i++) { scanf("%d%d",&point[i].x,&point[i].y); } int cnt=0; for(int i=0;i<n-1;i++) { for(int j=i+1;j<n;j++) { a[cnt].zx=point[i].x+point[j].x; a[cnt].zy=point[i].y+point[j].y; cnt++; } } sort(a,a+cnt,cmp); int sum=0; int num=1; for(int i=1;i<cnt;i++) { if(a[i].zx==a[i-1].zx&&a[i].zy==a[i-1].zy) { num++; } else { sum+=(num*(num-1)/2); num=1; } } printf("%d\n",sum); } return 0; }
cf-
Number of Parallelograms
You are given n points on a plane. All the points are distinct and no three of them lie on the same line. Find the number of parallelograms with the vertices at the given points.
The first line of the input contains integer n (1 ≤ n ≤ 2000) — the number of points.
Each of the next n lines contains two integers (xi, yi) (0 ≤ xi, yi ≤ 109) — the coordinates of the i-th point.
Print the only integer c — the number of parallelograms with the vertices at the given points.
4 0 1 1 0 1 1 2 0
1
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define INF 0x3f3f3f3f
#define ull unsigned long long
#define ll long long
#define IN __int64
#define N 2010
#define M 1000000007
using namespace std;
struct zz
{
double x;
double y;
}p[N];
bool cmp(zz a,zz b)
{
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}
struct ss
{
double x;
double y;
}pp[N*N];
bool cmp1(ss a,ss b)
{
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}
int main()
{
int n,i,j,k;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
sort(p,p+n,cmp);
k=0;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
pp[k].x=(p[i].x+p[j].x)/2;
pp[k++].y=(p[i].y+p[j].y)/2;
}
}
/*
for( i=0;i<k;i++){
cout<<pp[i].x<<" "<<pp[i].y<<endl;
}
*/
int cnt=1;
ll sum=0;
sort(pp,pp+k,cmp1);
for(i=0;i<k;i++)
{
if(pp[i].x==pp[i+1].x&&pp[i].y==pp[i+1].y)
cnt++;
else
{
sum+=((cnt-1)*cnt)/2;
cnt=1;
}
}
printf("%d\n",sum);
}
return 0;
}