2014-09-03 60 views
0

我需要查找數組中最大可能的數字總和,但數字必須從唯一數組索引中繪製...就像那樣:如何查找從唯一數組索引中繪製的數組中最大可能的數字總和

double maxSum = 0; 

double a = {1.0 , 2.0, 3.0}; 
double b = {4.0 , 5.0, 6.0}; 
double c = {7.0 , 8.0, 9.0}; 

sum = a[0] + b[1] + c[2]; 

// or sum = a[0] + b[2] + c[1] 
// or sum = a[1] + b[0] + c[2] 
// etc. - how to do that for i arrays and for j numbers in array? 

if(sum >=maxSum){ 
maxSum = sum; 
} 

這是我做過什麼 - 但沒有任何線索,下一步怎麼辦?

public ArrayList<Double[]> tempArrayCreator() { 
    ArrayList<Double[]> lists = new ArrayList<Double[]>(); 

    Double[] list1 = { 6.0, 7.0, 6.0 }; 
    Double[] list2 = { 4.5, 6.0, 6.75 }; 
    Double[] list3 = { 6.0, 5.0, 9.0 }; 

    lists.add(list1); 
    lists.add(list2); 
    lists.add(list3); 

    return lists; 
} 

public double maxPossibleSum(ArrayList<Double[]> lists) { 

    double result = 0.0; 

    for (int i = 0; i < lists.size(); i++) { 
     for (int j = 0; j < lists.get(i).length; j++) { 

     // ??? 

     } 
    } 

    return result; 

} 

編輯。例如:

list1 = { 1, 5, 2}; 
list2 = { 9, 3, 7}; 
list3 = { 8, 4, 9}; 

possible solutions: 
list1[0] + list2[1] + list3[2] = 13 
list1[0] + list2[2] + list3[1] = 12 
list1[1] + list2[0] + list3[2] = 23 <-- here it is! 
list1[1] + list2[2] + list3[0] = 20 
list1[2] + list2[0] + list3[1] = 15 
list1[2] + list2[1] + list3[0] = 13 
+0

爲什麼不從數組中添加值到您的結果變量? – 2014-09-03 12:14:35

+0

我是否正確地認爲:a)所有的源數組將具有相同數量的元素和b)每個唯一的索引,其中0 <=索引 GHC 2014-09-03 12:19:25

+0

我對你想要做的事情有點困惑。這聽起來像你想要從每個列表中總結出最大的數字?你能澄清你的預期結果嗎? – Moob 2014-09-03 12:22:14

回答

3

首先,你希望得到您的索引的所有排列的列表。

{[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 1, 0], [2, 0, 1]} 

例如,這answer提供了一種方式來做到這一點:

public static List<List<Integer>> getAllPermutations(int arraySize) { 
    List<Integer> elements = new ArrayList<Integer>(); 
    for (int i = 0; i < arraySize; i++) { 
     elements.add(i); 
    } 
    List<List<Integer>> result = new ArrayList<List<Integer>>(); 
    getAllPermutations(result, elements, 0); 
    return result; 
} 

private static void getAllPermutations(List<List<Integer>> result, List<Integer> elements, int k) { 
    for (int i = k; i < elements.size(); i++) { 
     java.util.Collections.swap(elements, i, k); 
     getAllPermutations(result, elements, k + 1); 
     java.util.Collections.swap(elements, k, i); 
    } 
    if (k == elements.size() - 1) { 
     result.add(new ArrayList<Integer>(elements)); 
    } 
} 

然後你通過所有的排列組合循環:

public double maxPossibleSum(ArrayList<Double[]> lists) { 
    List<List<Integer>> allPermutations = getAllPermutations(3); 
    double maxSum = Double.NEGATIVE_INFINITY; 
    for (List<Integer> permutation : allPermutations) { 
     double sum = 0; 
     for (int i = 0; i < permutation.size(); i++) { 
      Integer index = permutation.get(i); 
      sum += lists.get(i)[index]; 
     } 
     if (sum > maxSum) { 
      maxSum = sum; 
     } 
    } 
    return maxSum; 
} 
+0

是啊!這將工作 - 感謝你們所有人! – Aleizdein 2014-09-03 12:51:14

+0

請明確您的來源。否則提出的代碼是好的 – olivieradam666 2014-09-03 12:55:23

0
for (int i = 0; i < lists.size(); i++) { 
     Double[] tmp = list.get(i); 
     for (int j = 0; j < tmp.length; j++) { 
      result += tmp[j];  
     }   

    } 
0

內的for循環只需添加

result+=lists.get(i)[j]; 
+0

謝謝你 - 但以這種方式,我得到所有數組中的所有數字的總和 - 我想要的是從每個數組中獲得最大可能的總和(只有一個數組)(在獨特的數組索引中) – Aleizdein 2014-09-03 12:33:52

+0

您可以在您的問題中提供示例嗎?它相當困惑,明白你真的要求 – Vihar 2014-09-03 12:35:00