博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Number of Parallelograms(求平行四边形个数)
阅读量:7048 次
发布时间:2019-06-28

本文共 2845 字,大约阅读时间需要 9 分钟。

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;            }    }}

 

转载地址:http://fqzol.baihongyu.com/

你可能感兴趣的文章
&quot;undefined reference to&quot; 问题解决方法
查看>>
hadoop: hdfs API示例
查看>>
供应商地点信息更新(转)
查看>>
【转】使用DateFormat把时间长度格式化为"时:分:秒"格式--不错
查看>>
从统计学角度来看深度学习(2):自动编码器和自由能
查看>>
[Python]Unicode转ascii码的一个好方法
查看>>
jQuery用户数字评分效果
查看>>
Python爬行动物(一):基本概念
查看>>
solr4.5配置中文分词器mmseg4j
查看>>
MobaSSH SSH server for Windows - Download Home Edition
查看>>
文章之间的基本总结:Activity生命周期
查看>>
数据库sqlserver2008登陆名密码登陆不了怎么办?
查看>>
projecteuler----&gt;problem=19----Counting Sundays
查看>>
CSS鼠标样式整理
查看>>
移动加密那点事儿_值存储加密
查看>>
Unity3D之空间转换学习笔记(三):3D数学
查看>>
jquery.lazyload.js图片延迟加载
查看>>
Atitit.异步编程 java .net php python js 对照
查看>>
手机网站和PC网站兼容的响应式网页设计
查看>>
设计与实现模块管理系统基本功能定义自己(18--设计模块附件[1])
查看>>