zoukankan      html  css  js  c++  java
  • Number of Parallelograms(求平行四边形个数)

    Number of Parallelograms
    time limit per test
    4 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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
    题解:平行四边形的中点是确定的,那么根据两两边的中点,如果有重合那么可以组成平行四边形;
    代码:
    import java.util.Arrays;
    import java.util.Comparator;
    import java.util.Scanner;
    
    
    public class 平行四边形数 {
        static Scanner cin = new Scanner(System.in);
        public static void main(String[] argvs){
            int N = cin.nextInt();
            Point[] a = new Point[N];
            for(int i = 0; i < N; i++){
                a[i] = new Point();
                a[i].x = cin.nextDouble();
                a[i].y = cin.nextDouble();
                //a[i].Put();
            }
            Point[] b = new Point[N * (N + 1)/2];
            int tp = 0;
            for(int i = 0; i < N*(N + 1)/2; i++)
                    b[i] = new Point();
            for(int i = 0; i < N; i++){
                for(int j = i + 1; j < N; j++){
                    if(a[i].x == a[j].x && a[i].y == a[j].y)
                        continue;
                    b[tp++] = Point.getp(a[i], a[j]);
                }
            }
            
            Arrays.sort(b, 0, tp, new PointComparator());
    //        System.out.println("tp = " + tp);
            int ans = 0, cnt = 1;
            for(int i = 1; i < tp; i++){
            //    System.out.println(b[i].x + " " + b[i].y);
                if(b[i].x == b[i - 1].x && b[i].y == b[i - 1].y){
                    cnt++;
                }
                else{
                    ans += cnt * (cnt - 1) / 2;
                    cnt = 1;
                }
            }
            System.out.println(ans);
        }
    }
    
    class Point{
        static Scanner cin = new Scanner(System.in);
        public double x, y;
        public void Put(){
            x = cin.nextInt();
            y = cin.nextInt();
        //    System.out.println("***" + this.x + " " + this.y);
        }
        static Point getp(Point a, Point b){
            Point c = new Point();
            c.x = (a.x + b.x) / 2;
            c.y = (a.y + b.y) / 2;
        //    System.out.println("***" + c.x + " " + c.y);
            return c;
        }
    }
    class PointComparator implements Comparator{
        public int compare(Object x, Object y){
            Point a = (Point)x;
            Point b = (Point)y;
                if(a.x != b.x){
                    if(a.x <= b.x)
                        return -1;
                    else
                        return 1;
                }
                else{
                    if(a.y <= b.y)
                        return -1;
                    else
                        return 1;
                }
        }
    }
  • 相关阅读:
    利用connect建立前端开发服务器
    Bootstrap Popover 隐藏的Javasript方法
    Kafka 2.1.0压缩算法性能测试
    关于Kafka java consumer管理TCP连接的讨论
    Java API获取consumer group最新提交位移的时间
    关于Kafka producer管理TCP连接的讨论
    【译】Apache Kafka支持单集群20万分区
    关于Kafka broker IO的讨论
    Kafka 2.0 ConsumerGroupCommand新功能
    关于Kafka high watermark的讨论2
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5502341.html
Copyright © 2011-2022 走看看