java 平面上在一条直线上最多的点数

    /**
     * 在坐标系的第一象限上有N个点,请问:最多有多少个点在一条直线上?
     */

    public static void testPoints() {
        Point[] points = new Point[8];
        points[0] = new Point(1, 1);
        points[1] = new Point(2, 2);
        points[2] = new Point(3, 3);
        points[3] = new Point(4, 4);
        points[4] = new Point(2, 3);
        points[5] = new Point(3, 1);
        points[6] = new Point(3, 2);
        points[7] = new Point(1, 2);
        HashMap<Point, Integer> map = new HashMap();
        //在坐标系上划出来,明显是最多4个点
        //k : 斜率
        for (int i = 0; i < points.length; i++) {
            Point current = points[i];
            HashMap<Float, Integer> mapKofPoint = new HashMap();
            int vertical = 0;//垂直的,无k
            for (int j = 0; j < points.length; j++) {
                Point next = points[j];

                if (current.x == next.x && current.y == next.y) {
                    //self
                } else if (current.x == next.x) {
                    //无k
                    vertical++;
                } else {
                    float newK = (1f * (next.y - current.y)) / (next.x - current.x);
                    if (mapKofPoint.containsKey(newK)) {
                        mapKofPoint.put(newK, mapKofPoint.get(newK) + 1);
                    } else {
                        mapKofPoint.put(newK, 1);
                    }
                }
            }
            int result = 0;
            Iterator<Map.Entry<Float, Integer>> entryIterator = mapKofPoint.entrySet().iterator();
            while (entryIterator.hasNext()) {
                Map.Entry<Float, Integer> entry = entryIterator.next();
                if (entry.getValue() > result) {
                    result = entry.getValue();
                }

            }
            //加1是因为需要算上自己这个点
            result = Math.max(result,vertical)+1;
            map.put(current, result);
        }
        Iterator<Map.Entry<Point, Integer>> entryIterator = map.entrySet().iterator();
        while (entryIterator.hasNext()) {
            Map.Entry<Point, Integer> entry = entryIterator.next();
            System.out.println("testPoints--key=" + entry.getKey() + "--num=" + entry.getValue());
        }
    }


12-13 16:57:51.939 7083-7083/com.example.bxh.sayhello I/System.out: testPoints--key=Point(1, 2)--num=3
12-13 16:57:51.939 7083-7083/com.example.bxh.sayhello I/System.out: testPoints--key=Point(2, 3)--num=2
12-13 16:57:51.939 7083-7083/com.example.bxh.sayhello I/System.out: testPoints--key=Point(3, 2)--num=3
12-13 16:57:51.939 7083-7083/com.example.bxh.sayhello I/System.out: testPoints--key=Point(1, 1)--num=4
12-13 16:57:51.939 7083-7083/com.example.bxh.sayhello I/System.out: testPoints--key=Point(3, 1)--num=3
12-13 16:57:51.939 7083-7083/com.example.bxh.sayhello I/System.out: testPoints--key=Point(3, 3)--num=4
12-13 16:57:51.939 7083-7083/com.example.bxh.sayhello I/System.out: testPoints--key=Point(4, 4)--num=4
12-13 16:57:51.939 7083-7083/com.example.bxh.sayhello I/System.out: testPoints--key=Point(2, 2)--num=4





  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值