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;
                }
        }
    }
  • 相关阅读:
    Tcpdump抓包
    关于Adroid Bitmap OutOfMemoryError的问题解决
    java用substring函数截取string中一段字符串
    偶耶DIY布偶成都实体店开业
    瑞士Kardex(卡迪斯)自动化仓储货柜,Shuttle XP系列升降库驱动监控系统
    360顽固木马专杀工具 千万别用 会删除Oracle服务
    天上人和酒店管理系统(.net3.5 + sql2000 + linq to sql)
    [转]VC++中CListCtrl listcontrol用法技巧
    [转]孙鑫教程学习笔记
    [转]VC2005从开发MFC ActiveX ocx控件到发布到.net网站的全部过程
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5502341.html
Copyright © 2011-2022 走看看