Light oj ---1058---poj---1971---Parallelogram Counting

 

题目链接: http://poj.org/problem?id=1971

Mean:

给定平面上的n个点,求这n个点中能构成平行四边形的个数。

analyse:

由于平行四边形的两条对角线交于一点,且该点为两对角线的中点。若两线段的中点是同一个点,则这两条线段的四个顶点一定可以构成一个平行四边形!

所以可以求所有线段的中点,然后根据相同中点的个数来判断平行四边形的个数。如果一个点重复了k次,则形成的平行四边形的个数为k(k-1)/2.

light oj AC

 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include<vector>
#include<queue>
#include<algorithm>

using namespace std;
typedef long long LL;

const int maxn=1009;
const int INF=0x3f3f3f3f;
const int mod=2009;
int sum[maxn];

int n;
struct node
{
    int x, y;
} a[maxn*maxn], s[maxn];
int num;

int cmp(node A, node B)
{
    if(A.x != B.x)
        return A.x < B.x;
    return A.y < B.y;
}

int main()
{
    int T, cas=1;
    scanf("%d", &T);

    while(T--)
    {
        scanf("%d", &n);
        for(int i=0; i<n; i++)
            scanf("%d %d", &s[i].x, &s[i].y);

        int k=0, ans=1, sum=0;
        for(int i=0; i<n; i++)
        {
            for(int j=i+1; j<n; j++)
            {
                a[k].x=(s[i].x+s[j].x);
                a[k++].y=(s[i].y+s[j].y);
            }
        }
        sort(a, a+k, cmp);

        for(int i=0; i<k; i++)
        {
            if(a[i].x==a[i+1].x&&a[i].y==a[i+1].y)
                ans++;
            else
            {
                ans=ans*(ans-1)/2;
                sum+=ans;
                ans=1;
            }
        }
        printf("Case %d: %d\n", cas++, sum);
    }
    return 0;
}

输出少了点而已

poj AC

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include<vector>
#include<queue>
#include<algorithm>

using namespace std;
typedef long long LL;

const int maxn=1009;
const int INF=0x3f3f3f3f;
const int mod=2009;
int sum[maxn];

int n;
struct node
{
    int x, y;
} a[maxn*maxn], s[maxn];
int num;

int cmp(node A, node B)
{
    if(A.x != B.x)
        return A.x < B.x;
    return A.y < B.y;
}

int main()
{
    int T;
    scanf("%d", &T);

    while(T--)
    {
        scanf("%d", &n);
        for(int i=0; i<n; i++)
            scanf("%d %d", &s[i].x, &s[i].y);

        int k=0, ans=1, sum=0;
        for(int i=0; i<n; i++)
        {
            for(int j=i+1; j<n; j++)
            {
                a[k].x=(s[i].x+s[j].x);
                a[k++].y=(s[i].y+s[j].y);
            }
        }
        sort(a, a+k, cmp);

        for(int i=0; i<k; i++)
        {
            if(a[i].x==a[i+1].x&&a[i].y==a[i+1].y)
                ans++;
            else
            {
                ans=ans*(ans-1)/2;
                sum+=ans;
                ans=1;
            }
        }
        printf("%d\n", sum);
    }
    return 0;
}

 

posted @ 2016-08-23 17:29  爱记录一切美好的微笑  阅读(155)  评论(0编辑  收藏  举报