gpt4 book ai didi

java - 找到位于这些元素之间距离最小的位置

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:05:46 25 4
gpt4 key购买 nike

我尝试实现位于这些元素之间的距离最小的位置?我在 Java 11 中有这段代码:

public class Main {
public static void main(String[] args) {
int [] arr= {5, 50, 3, 42, 18, 16, 8, 30, 44}; // Array
menorD l = new menorD();
menorD.MinD(arr);
}
}

public class menorD {

static int arr_zise;

public static void minD (int [] arr) {
arr_zise = arr.length;
int i, j;
int minD=0;

for(i=0;i<arr_zise; i++) {
for(j=0;j<arr_zise; j++) {
if(arr[i]!=arr[j]) {
minD=arr[i]-arr[j];
System.out.print(" i="+ arr[i]+ " j="+ arr[j]+ " minD es: "+Math.abs(min));
System.out.println();
}
}
}
}
}

我试着找到这个:

arr = {5, 50, 3, 42, 18, 16, 8, 30, 44}

在这种情况下,我的 Dmin 将是它们之间距离较小的数字之间的差异,

Dmin1 = 5-3 = 2;

Dmin2 = 18-16 = 2;

Dmin3 44-42 = 2;

不重复数组中数字的索引。我已经制作了这段代码,但我很难找到我要找的东西。

最佳答案

理想情况下,当处理逻辑上组合在一起的数据时,您应该抽象出一个类来封装它。在您的情况下,您想跟踪所有可能的 distance组合。每个Combination应该跟踪:

  1. 值的索引。
  2. 重视自己。
  3. 距离。
  4. 哪个低哪个高。

其中 3. 和 4. 可以从 1. 和 2. 计算得出

    class Combination {
int indexA, indexB, valueA, valueB;

public Combination(int[] array, int indexA, int indexB) {
this.indexA = indexA;
this.indexB = indexB;
this.valueA = array[indexA];
this.valueB = array[indexB];
}

public int getDistance() { ... }
public int getHigh() { ... }
public int getLow() { ... }
public int getHighIndex() { ... }
public int getLowIndex() { ... }
}

有了这样的数据结构(类)可用,您可以为每个可能的组合构造对象(当然没有重复 - 注意 j 如何从 i + 1 开始可变而不是重复可能的组合):

        List<Combination> combinations = new ArrayList<>();
for (int i = 0; i < array.length; i++)
for (int j = i + 1; j < array.length; j++)
combinations.add(new Combination(array, i, j));

然后使用这个 ListCombination你可以计算它们之间的最小距离:

        int min = combinations.stream()
.mapToInt(Combination::getDistance)
.min().getAsInt();

最后,您可以选择那些与先前计算的最小距离相匹配的组合:

        combinations.stream()
.filter(c -> c.getDistance() == min)
.forEach(c -> System.out.println(c));

关键是拥有 Combination类在其自己的封装类中抽象出来,因此它可以单独负责提供必要的 API 来检查特定组合:索引、值、距离、高值、低值甚至 String ( toString ) 表示。

下面是这个方法的一个完整的工作演示,运行它来感受一下:

import java.util.ArrayList;
import java.util.List;

public class MinimumDistance {

public static void main(String[] args) {
printMinimums(5, 50, 3, 42, 18, 16, 8, 30, 44);
}

public static void printMinimums(int... array) {
List<Combination> combinations = new ArrayList<>();
for (int i = 0; i < array.length; i++)
for (int j = i + 1; j < array.length; j++)
combinations.add(new Combination(array, i, j));

int min = combinations.stream()
.mapToInt(Combination::getDistance)
.min().getAsInt();

combinations.stream()
.filter(c -> c.getDistance() == min)
.forEach(c -> System.out.println(c));
}

static class Combination {
int indexA, indexB, valueA, valueB;

public Combination(int[] array, int indexA, int indexB) {
this.indexA = indexA;
this.indexB = indexB;
this.valueA = array[indexA];
this.valueB = array[indexB];
}

public int getDistance() {
return getHigh() - getLow();
}

public boolean isValueAHigh() {
return valueA > valueB;
}

public int getHigh() {
return isValueAHigh() ? valueA : valueB;
}

public int getLow() {
return isValueAHigh() ? valueB : valueA;
}

public int getHighIndex() {
return isValueAHigh() ? indexA : indexB;
}

public int getLowIndex() {
return isValueAHigh() ? indexB : indexA;
}

public String toString() {
return String.format("%d[%d] - %d[%d] = %d",
getHigh(), getHighIndex(),
getLow(), getLowIndex(),
getDistance());
}
}
}

Complete code on GitHub

希望这对您有所帮助。

关于java - 找到位于这些元素之间距离最小的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56204034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com