zoukankan      html  css  js  c++  java
  • 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
    ", 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
    ", sum);
        }
        return 0;
    }
  • 相关阅读:
    linux socket里的send和recv,阻塞与非阻塞socket、TCP与UDP在这方面的区别
    leetcode 149 Max Points on a Line
    leetcode 126 Word Ladder II
    leetcode 123 Best Time to Buy and Sell Stock III
    LC 297 Serialize and Deserialize Binary Tree
    栈和队列问题
    链表问题
    day17--权限管理和配置服务
    谷粒学院功能简介及系统架构
    day01--MybatisPlus的使用
  • 原文地址:https://www.cnblogs.com/w-y-1/p/5800266.html
Copyright © 2011-2022 走看看