java - 创建火车线路上所有车站的距离图

标签 java

我正在复习编程入门考试,我有一个问题,在之前的试卷中我有点卡住了。

问题:

Write a method that takes a double array as an argument with values representing the positions of train stations along a track. The method should return a two-dimensional array with the distances between each pair of stations in the argument. The array of distances should have only one entry for each pair of stations (i.e. do not use a rectangular array).

我有一个问题的解决方案,但我就是无法得到最后一点,每一对应该只有一个条目。我曾考虑过创建一个查找表,其中包含所有条目,以查看两个站点的距离,但是数组中会有很多用于后面站点的空单元格,因为距离已经计算出来了。

这是我当前的解决方案

//Set of locations on the train line
private static double[] stations = {0.0, 2.0, 3.0, 5.0};

//Method to take the array of doubles and create distance map
public static double[][] getDistances(double[] st){
    double[][] distanceMap = new double[st.length][st.length-1];
    int x;
    for(int i=0; i<st.length; i++){
        x=0;
        for(int j=0; j<st.length; j++){
            if(j != i){
                distanceMap[i][x] = Math.abs(st[i]-st[j]); 
                x++;
            }
        }
    }
    return distanceMap;
}

//Main method to get the distance map then loop over results
public static void main(String[] args){
    double[][] arrayMatrix = getDistances(stations);

    for(int i=0; i<arrayMatrix.length; i++){
        for(int j=0; j<arrayMatrix[0].length; j++){
            System.out.print(arrayMatrix[i][j]+"  ");
        }
        System.out.println("");
    }

}

如果有人能指出我正确的方向,我将不胜感激。

提前致谢。

//编辑

经过 @izomorphius 的一些很好的建议,我成功解决了这个问题。谢谢。

这是完整的解决方案

//Set of locations on the train line
private static double[] stations = {0.0, 2.0, 3.0, 5.0};

//Method to take the array of doubles and create distance map
public static double[][] getDistances(double[] st){
    double[][] distanceMap = new double[st.length-1][];
    int size = st.length-1;

    for(int i=0; i<distanceMap.length; i++){
        distanceMap[i] = new double[size];
        size--;
    }

    ArrayList<String> lut = new ArrayList<String>();

    int x;
    for(int i=0; i<distanceMap.length; i++){
        x=0;
        for(int j=0; j<st.length; j++){
            if(j != i && !lut.contains(i+"/"+j)){
                distanceMap[i][x] = Math.abs(st[i]-st[j]); 
                lut.add(i+"/"+j);
                lut.add(j+"/"+i);
                x++;
            }
        }
    }
    return distanceMap;
}

//Main method to get the distance map then loop over results
public static void main(String[] args){
    double[][] arrayMatrix = getDistances(stations);

    for(int i=0; i<arrayMatrix.length; i++){
        for(int j=0; j<arrayMatrix[i].length; j++){
            System.out.print(arrayMatrix[i][j]+"  ");
        }
        System.out.println("");
    }

}

最佳答案

该声明的意思是“即不要使用矩形阵列”。这个想法是为每一对仅存储一个值。例如,如果有一对 (a,b) 且 a < b 将 a 和 b 之间的距离存储在 a 的数组中,但不存储在 b 的数组中。因此,第一个站的数组大小为 n - 1(到所有其他站的距离),第二个站的数组大小为 n - 2(除了第一个站之外的所有其他站),依此类推。因此你的数组将是三角形而不是矩形。我希望这个提示足够了,因为毕竟我的想法不是让我解决您的问题。

关于java - 创建火车线路上所有车站的距离图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10532322/

相关文章:

java - 解密 DES/CBC/ZeroBytePadding 数据

Java 8 - 类与其功能之一之间的映射

Java - 针对白名单的手动对象验证

java - OpenCV - 使用 Java 去除图像中的噪声

java - 如何从格式不正确的xml中获取值

java - 创建的 Runnables 的 lambda 的不同行为

java - 如何维护生产和开发不同的maven环境?

java - 我们如何获取父包注释?有人提供示例吗?

java - maven 和 jboss 模块

java - 解压 zip 文件时出错