【问题标题】:How to find the maximum possible sum of numbers in arrays drawn from a unique array index如何在从唯一数组索引中提取的数组中找到最大可能的数字总和
【发布时间】:2014-10-27 22:28:30
【问题描述】:

我需要在数组中找到最大可能的数字总和,但数字必须从唯一的数组索引中提取......就像这样:

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

【问题讨论】:

  • 为什么不将数组中的值添加到结果变量中?
  • 我的想法是否正确:a)所有源数组都将具有相同数量的元素,并且 b)0
  • 我对你想要做什么有点困惑。听起来您想将每个列表中的最大数字相加?你能澄清一下你的预期结果吗?
  • 我认为它是在代码中的 cmets 中指定的“如何为 i 数组和数组中的 j 数字执行此操作?”
  • @Moob 我想从唯一数组索引中提取的数字中获得最大总和 - 在我的示例中,我应该得到 21.0 - list1[0]+list2[1]+list3[2]

标签: java arrays


【解决方案1】:
    for (int i = 0; i < lists.size(); i++) {
        Double[] tmp = list.get(i);
        for (int j = 0; j < tmp.length; j++) {
            result += tmp[j];     
        }           

    }

【讨论】:

    【解决方案2】:

    在你的 for 循环中添加

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

    【讨论】:

    • 谢谢你 - 但这样我得到了所有数组中所有数字的总和 - 我想要的是从每个数组中获得最大可能的总和(并且在唯一的数组索引中)以及)
    • 你能举个例子吗?很难理解你真正想要的是什么
    【解决方案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;
    }
    

    【讨论】:

    • 请明确您的来源。否则建议的代码很好
    猜你喜欢
    • 2014-04-18
    • 2022-01-12
    • 1970-01-01
    • 2019-08-21
    • 1970-01-01
    • 2014-03-05
    • 2018-04-25
    • 2016-08-21
    • 2014-05-19
    相关资源
    最近更新 更多