Problem 2231 平行四边形数
Accept: 313 Submit: 1086
Time Limit: 2000 mSec Memory Limit : 32768 KB
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<iostream>
#include<algorithm>
using namespace std;
struct node{
int x,y;
}a[505];
struct point{
int x,y;
bool operator<(const point &a)const{
if(a.x!=x){
return a.x<x;
}
else return a.y<y;
}
}b[505*505];
int main(){
int n,k;
while(cin>>n){
for(int i=0;i<n;i++){
cin>>a[i].x>>a[i].y;
}
k=0;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
b[k].x = a[i].x + a[j].x;
b[k].y = a[i].y + a[j].y;
k++;
}
}
sort(b,b+k);
int cnt=0,sum=0;
for(int i=0;i<k-1;i++){
if(b[i].x==b[i+1].x&&b[i].y==b[i+1].y){
cnt++;
}
else{
sum+=cnt*(cnt+1)/2;
cnt=0;
}
}
cout<<sum<<endl;
}
return 0;
}