如何找到号码的最大可能总和在从一个唯一的数组索引绘制阵列 [英] How to find the maximum possible sum of numbers in arrays drawn from a unique array index

查看:108
本文介绍了如何找到号码的最大可能总和在从一个唯一的数组索引绘制阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到数字的最大可能的总和阵列,但数量必须从一个独特的数组索引可以得出......就是这样:

 双maxSum = 0;双A = {1.0,2.0,3.0};
双B = {4.0,5.0,6.0};
双c = {7.0,8.0,9.0};总和=一个[0] + B [1] + C [2];//或总和=一个[0] + B [2] + C [1]
//或总和=一[1] + B [0] + C [2]
//等等 - 如何做到这一点,因为我阵列和对J个数字阵列?如果(总和> = maxSum){
maxSum =总和;
}

这是我做过什么 - 但没有任何线索下一步该怎么做...

 公开的ArrayList<双[]> tempArrayCreator(){
    ArrayList的<双[]>名单=新的ArrayList<双[]>();    双[] list1的= {6.0,7.0,6.0};
    双[] =列表2 {4.5,6.0,6.75};
    双[] =项目list3 {6.0,5.0,9.0};    lists.add(list1的);
    lists.add(列表2);
    lists.add(项目list3);    返回列表;
}公共双maxPossibleSum(ArrayList的<双[]>名单){    双重结果= 0.0;    的for(int i = 0; I< lists.size();我++){
        对于(INT J = 0; J< lists.get(I)。长度; J ++){        //?        }
    }    返回结果;}

编辑。例如:

  list1的= {1,5,2};
列表2 = {9,3,7};
项目list3 = {8,4,9};可能的解决方案:
list1的[0] +列表2 [1] +项目list3 [2] = 13
list1的[0] +列表2 [2] +项目list3 [1] = 12
list1的[1] +列表2 [0] +项目list3 [2] = 23℃ - 在这里它是!
list1的[1] +列表2 [2] +项目list3 [0] = 20
list1的[2] +列表2 [0] +项目list3 [1] = 15
list1的[2] +列表2 [1] +项目list3 [0] = 13


解决方案

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

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

例如,这回答给出了一个办法做到这一点:

 公共静态列表<名单,LT;整数GT;> getAllPermutations(INT ARRAYSIZE){
    清单<整数GT;元素=新的ArrayList<整数GT;();
    的for(int i = 0; I< ARRAYSIZE;我++){
        elements.add(ⅰ);
    }
    清单<名单,LT;整数GT;>结果=新的ArrayList<名单,LT;整数GT;>();
    getAllPermutations(结果,元件,0);
    返回结果;
}私有静态无效getAllPermutations(列表<名单,LT;整数GT;>因此,列表与LT;整数GT;元素,诠释K){
    对于(INT I = K,I< elements.size();我++){
        java.util.Collections.swap(元素,I,K);
        getAllPermutations(结果,元件,K + 1);
        java.util.Collections.swap(元素,K,I);
    }
    如果(K == elements.size() - 1){
        result.add(新的ArrayList<整数GT;(元素));
    }
}

然后你通过所有的排列组合迭代:

 公共双maxPossibleSum(ArrayList的<双[]>名单){
    清单<名单,LT;整数GT;> allPermutations = getAllPermutations(3);
    双maxSum = Double.NEGATIVE_INFINITY;
    对于(列表<整数GT;置换:allPermutations){
        双总和= 0;
        的for(int i = 0; I< permutation.size();我++){
            整数指数= permutation.get(ⅰ);
            总和+ = lists.get(一)[索引];
        }
        如果(总和> maxSum){
            maxSum =总和;
        }
    }
    返回maxSum;
}

I need to find maximum possible sum of numbers in arrays but number must be drawn from a unique array index...just like that:

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;
} 

this is what I did - but don't have any clue what to do next...

    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;

}

edit. example:

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

解决方案

First, you want to get the list of all the Permutations of your indexes.

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

For example, this answer gives a way to do it:

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));
    }
}

Then you iterate through all your permutations:

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;
}

这篇关于如何找到号码的最大可能总和在从一个唯一的数组索引绘制阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆