2014-09-03 59 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