FZU-2231-平行四边形数 cf-Number of Parallelograms

计算数学-题目点击打开链接

Problem Description

在一个平面内给定n个点,任意三个点不在同一条直线上,用这些点可以构成多少个平行四边形?一个点可以同时属于多个平行四边形。

Input

多组数据(<=10),处理到EOF。

每组数据第一行一个整数n(4<=n<=500)。接下来n行每行两个整数xi,yi(0<=xi,yi<=1e9),表示每个点的坐标。

Output

每组数据输出一个整数,表示用这些点能构成多少个平行四边形。

Sample Input


0 1 
1 0 
1 1 
2 0 
Sample Output


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.

Input

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.

Output

Print the only integer c — the number of parallelograms with the vertices at the given points.

Example
Input
4
0 1
1 0
1 1
2 0
Output
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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值