将最小和最大成对找到Java

时间:2018-06-20 11:18:23

标签: java class methods coordinates

我希望给定一个点(任意数量的点)的程序从所有点中获取笛卡尔平面中的最近点和最远点,然后计算最接近最远点的距离。

我有下面的代码查找最小x和最小y,以及最大x和最大y,但是我希望两个min是成对的,而两个max是成对的。我在某些测试数据上没有得到正确的答案,但是在某些测试数据上却得到了正确的答案。

为简单起见,我只考虑笛卡尔平面中的正x和y。

import java.lang.Math;
import java.util.Scanner;

class Point
{
    double x;
    double y;
}
class Compare
{
    Double maxx;
    Double maxy;
    Double minx;
    Double miny;

    public void maxx(double num)
    {
        if (maxx == null)
            maxx = num;
        else if(num > maxx)
            maxx = num;
    }
    public void maxy(double num)
    {
        if (maxy == null)
            maxy = num;
        else if(num > maxy)
            maxy = num;
    }
    public void minx(double num)
    {
        if (minx == null)
            minx = num;
        else if(num < minx)
            minx = num;
    }
    public void miny(double num)
    {
        if (miny == null)
            miny = num;
        else if(num < miny)
            miny = num;
    }
}
class Main
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        Compare compare = new Compare();

        int n = input.nextInt();

        if(n>=2 && n<=30)
        {
            for (int i = 0; i < n; i++) 
            {
                Point points = new Point();

                points.x = input.nextDouble();
                points.y = input.nextDouble();

                compare.minx(points.x);
                compare.miny(points.y);
                compare.maxx(points.x);
                compare.maxy(points.y);
            }
        }
        double dis;
        dis=Math.sqrt((compare.maxx-compare.minx)*(compare.maxx-compare.minx) + (compare.maxy-compare.miny)*(compare.maxy-compare.miny));
        System.out.printf("%.4f\n",dis);
    }
}

1 个答案:

答案 0 :(得分:1)

Math.hypot的用例。

当最小和最大相对于原点(0,0)时:

    Point min = null;
    double minDistance = Double.MAX_VALUE;
    Point max = null;
    double maxDistance = 0.0;
    for (int i = 0; i < n; i++) {
        Point point = new Point();

        point.x = input.nextDouble();
        point.y = input.nextDouble();

        double distanceToO = Math.hypot(point.x, point.y);
        if (min == null || minDistance > distanceToO) {
            minDistance = distanceToO;
            min = point;
        }
        if (max == null || maxDistance < distanceToO) {
            maxDistance = distanceToO;
            max = point;
         }
     }

当最小值和最大值涉及两个读取点之间的距离时:

    Point minFrom = null;
    Point minTo = null;
    double minDistance = Double.MAX_VALUE;

    Point maxFrom = null;
    Point maxTo = null;
    double maxDistance = 0.0;

    Point[] points = new Point[n];
    for (int i = 0; i < n; i++) {
        Point point = new Point();
        point.x = input.nextDouble();
        point.y = input.nextDouble();
        points[i] = point;
    }
    for (int i = 0; i < n; i++) {
        Point pointI = points[i];
        for (int j = i + 1; j < n; j++) {
            Point pointJ = points[j];
            double distance = Math.hypot(
                    pointJ.x - pointI.x,
                    pointJ.y - pointI.y);
            if (minFrom == null || minDistance > distance) {
                minDistance = distance;
                minFrom = pointI;
                minTo = pointJ;
            }
            if (maxFrom == null || maxDistance < distance) {
                maxDistance = distance;
                maxFrom = pointI;
                maxTo = pointJ;
            }
        }
    }