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 map = new HashMap();
        //在坐标系上划出来,明显是最多4个点
        //k : 斜率
        for (int i = 0; i < points.length; i++) {
            Point current = points[i];
            HashMap 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> entryIterator = mapKofPoint.entrySet().iterator();
            while (entryIterator.hasNext()) {
                Map.Entry entry = entryIterator.next();
                if (entry.getValue() > result) {
                    result = entry.getValue();
                }

            }
            //加1是因为需要算上自己这个点
            result = Math.max(result,vertical)+1;
            map.put(current, result);
        }
        Iterator> entryIterator = map.entrySet().iterator();
        while (entryIterator.hasNext()) {
            Map.Entry 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





你可能感兴趣的:(算法)