2012-05-10 7 views
1

私はプログラミング試験の入門書を改訂しています。私は以前の試験紙から少し詰まっています。列車内のすべての駅の距離地図を作成

質問:

は、トラックに沿って駅の位置を表す値を持つ引数としてdouble配列を受け取るメソッドを記述します。このメソッドは、引数内の各ステーションの対の間の距離を持つ2次元配列を返さなければなりません。距離の配列は、ステーションの各対に対して1つのエントリしか持たない(すなわち、長方形の配列を使用しない)。

私は問題の解決方法を持っていますが、各ペアに1つのエントリしかない最後のビットを取得できません。私はルックアップテーブルを作成することについて考えているので、2つのステーションの距離が、後で距離が既に計算されているので、配列は後のステーションのための空のセルをたくさん持っているかどうかを確認するためにすべてのエントリです。

はここで誰もが正しい方向に私を指すことができれば、それは本当にいただければ幸い私の現在のソリューション

//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からいくつかの素晴らしいアドバイスをした後

// EDIT

。ありがとう。

はここの文は、「すなわち矩形配列を使用していない」と言う何の完全なソリューション

//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(""); 
    } 

} 

答えて

2

です。アイデアは、各ペアに1つの値だけを格納することです。たとえば、ペア(a、b)と<bがaの配列にaとbの間の距離を格納しますが、bの配列には格納されません。したがって、第1のステーションのアレイは、サイズn-1(他のすべてのステーションまでの距離)であり、第2のステーションは、サイズn-2(第1のステーションを除く他のすべてのステーション)などである。そのため、配列は長方形ではなく三角形になります。私はこのヒントがあなたの問題を解決してくれるわけではないので、このヒントが十分であることを願っています。

+0

+1 @TrueWheel、私はこの要求の目的は、あなたが動的に割り当てられた配列をどのように扱うか見ることだと思う。 – giorashc

+0

ありがとう!私はあなたが三角形の配列を作ることができるとは決して考えませんでした。私は私の質問の一番下に私のソリューションを追加しました。 – TrueWheel

関連する問題