我需要查找数组中最大可能的数字总和,但数字必须从唯一数组索引中绘制...就像那样:如何查找从唯一数组索引中绘制的数组中最大可能的数字总和
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
为什么不从数组中添加值到您的结果变量? – 2014-09-03 12:14:35
我是否正确地认为:a)所有的源数组将具有相同数量的元素和b)每个唯一的索引,其中0 <=索引
GHC
2014-09-03 12:19:25
我对你想要做的事情有点困惑。这听起来像你想要从每个列表中总结出最大的数字?你能澄清你的预期结果吗? – Moob 2014-09-03 12:22:14