JavaRush /Java блог /Архив info.javarush /Кухня(); Задание N42.
terranum
28 уровень
Milan

Кухня(); Задание N42.

Статья из группы Архив info.javarush
Кухня(); Задание N42. - 1 Правила [Одномерные массивы] 42. Даны координаты n точек на плоскости: (Х1, Y2), ..., (Хn, Yn) (n ≤ 30). Найти номера пары точек, расстояние между которыми наибольшее (считать, что такая дара единственная). Кухня(); Задание N42. - 2
Комментарии (7)
ЧТОБЫ ПОСМОТРЕТЬ ВСЕ КОММЕНТАРИИ ИЛИ ОСТАВИТЬ КОММЕНТАРИЙ,
ПЕРЕЙДИТЕ В ПОЛНУЮ ВЕРСИЮ
Airon Уровень 34
15 сентября 2014
public static int[] maxDistancePoint(Point... array) {
    if (array.length < 2)
        throw new IllegalArgumentException("Bad array");
    int[] pair = new int[]{0, 1};
    for (int i = 0; i < array.length - 1; i++)
        for (int j = i + 1; j < array.length; j++) 
            if (array[pair[0]].getDistance(array[pair[1]]) < array[i].getDistance(array[j]))
                pair = new int[]{i, j};
    return pair;
}

public static class Point {
    public double x;
    public double y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getDistance(Point p) {
        return Math.hypot(x - p.x, y - p.y);
    }

    @Override
    public String toString() {
        return new StringBuffer().append("[").append(x).append(", ").append(y).append("]").toString();
    }
}
Docktor91 Уровень 40
9 сентября 2014
double x = coords[0][i] — coords[1][j];
мне кажется или там где 1 должен быть ноль)
а так все отлично, у меня в принципе только такая идея и была
Vash_the_Stampede Уровень 11
9 сентября 2014

    public static void solve(double[][] coords) {
        // предпологается, что double[][] = double[2][n], ибо так памяти меньше занимает
        int p1 = 0, p2 = 0;
        double max = Double.MIN_VALUE;
        for (int i = 1; i < coords[0].length; i++) {
            for (int j = 0; j < i; j++) {
                double x = coords[0][i] - coords[1][j];
                double y = coords[1][i] - coords[1][j];
                double l = Math.hypot(x, y);
                if (max < l) {
                    max = l;
                    p1 = i;
                    p2 = j;
                }
            }
        }
        System.out.println(p1 + " " + p2);
    }
Vash_the_Stampede Уровень 11
9 сентября 2014
kk
Vash_the_Stampede Уровень 11
9 сентября 2014
мне что-то в голову только перебор всех расстояний лезет. так и надо?