我试图达到这些元素之间距离最小的位置?我在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;而不重复数组中数字的索引。我做了这个代码,但我很难找到我要找的东西。
发布于 2019-05-19 08:18:06
理想情况下,当处理逻辑分组的数据时,您应该抽象出一个类来封装它。在您的情况下,您希望跟踪所有可能的distance组合。每个Combination都应该跟踪:
在这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));然后,使用这个List of Combination,您可以计算出它们之间的最小距离:
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());
}
}
}希望这能有所帮助。
发布于 2019-05-19 04:38:44
我在网上发现了这个。你在做这个练习吗?
参考资料:https://www.geeksforgeeks.org/find-the-minimum-distance-between-two-numbers/
public class MinimumDistance {
int minDist(int arr[], int n, int x, int y)
{
int i, j;
int min_dist = Integer.MAX_VALUE;
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if ((x == arr[i] && y == arr[j]
|| y == arr[i] && x == arr[j])
&& min_dist > Math.abs(i - j))
min_dist = Math.abs(i - j);
}
}
return min_dist;
}
public static void main(String[] args)
{
MinimumDistance min = new MinimumDistance();
int arr[] = {3, 5, 4, 2, 6, 5, 6, 6, 5, 4, 8, 3};
int n = arr.length;
int x = 3;
int y = 6;
System.out.println("Minimum distance between " + x + " and " + y
+ " is " + min.minDist(arr, n, x, y));
}
}发布于 2019-05-19 05:16:39
在遍历for循环时,可以将Map of Integer距离填充到索引Map<Integer,List<Integer>>的List (甚至根据用例的实际值)。如果您总是将这两个索引一起添加到List中,那么您就知道它们在List中总是相邻的,以便以后作为对进行检索。然后,只需使用您的最小距离作为地图的关键,以拉出相邻对的列表。请记住,正如所描述的,这个列表将有重复的。
https://stackoverflow.com/questions/56204034
复制相似问题