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