java - java - 获取2D数组的最大总和

117 2

有一个2D数组,int[][] arr= {{1,3,4,1},{5,7,8,9},{6,1,2,1}} ,我希望获得每列的总和,并且获得最大数量。最终应该返回{5,7,8,9} ,谁能帮我解决这个问题

时间: 原作者:

63 5

如果需要最大列,那么我有一个解决方案:


import java.util.Arrays;



public class ArrayTest {


 public static void main(String args[]) {


 /*


 * 1 3 4 1 


 * 5 7 8 9 


 * 6 1 2 1


 * 


 */



 int[][] arr = {{1, 3, 4, 1}, {5, 7, 8, 9}, {6, 1, 2, 1}};



 int m = arr.length;


 int n = arr[0].length;


 int[] arr2 = new int[n];


 int p = 0;



 int[][] colArray = new int[n][m];



 for (int i = 0; i < n; i++) {


 int[] arr_i = new int[m];


 //System.out.println("i =" + i);


 //System.out.println("p =" + p);


 int sum = 0;


 for (int j = 0; j < m; j++) {


 arr_i[j] = arr[j][p];


 sum += arr_i[j];


 }


 //System.out.println("Col:" + p +" :" + Arrays.toString(arr_i));


 colArray[i] = arr_i;


 arr2[p] = sum;


 p++;



 }


 System.out.println("Sum:" + Arrays.toString(arr2));


 int k = 0;


 int max = arr2[0];


 for (int i = 0; i < 3; i++) {


 if (arr2[i] > max) {


 max = arr2[i];


 k = i;


 }


 }


 System.out.println("Column index for max:" + k);


 System.out.println("Column:" + Arrays.toString(colArray[k]));


 }


}



输出:


Sum: [12, 11, 14, 11]


Column index for max: 2


Column: [4, 8, 2]



原作者:
124 2

我建议你将问题分解为更小的部分,用一个函数解决每个部分,然后将所有内容合并到一起。

解决方案示例


public class Main {



 public static long sum(int[] a){


 long sum = 0;


 for (int i : a) {


 sum = sum + i;


 }


 return sum;


 }



 public static int[] withMaxSumOf(int[][] as){


 // keep one sum for each array


 long[] sums = new long[as.length];



 // calculate sums


 for (int i = 0; i < as.length; i++) {


 int[] a = as[i];


 sums[i] = sum(a);


 }



 // find the biggest one


 int maxIndex = 0;


 long maxSum = sums[0];


 for (int i=1;i<sums.length;i++){


 if (sums[i] > maxSum){


 maxSum = sums[i];


 maxIndex = i;


 }


 }



 // return array that had biggest sum


 return as[maxIndex];



 }



 public static void main(String[] args){



 int[][] arr= {{1,3,4,1},{5,7,8,9},{6,1,2,1}};



 // find the one with max sum


 int[] max = withMaxSumOf(arr);



 // print it


 for (int i = 0; i < max.length; i++) {


 int x = max[i];


 if (i > 0) System.out.print(",");


 System.out.print(x);


 }



 System.out.println();



 }



}




原作者:
100 4

public static void main( String args[] ) {


 int[][] arr = { { 1, 3, 4, 1 }, { 5, 7, 8, 9 }, { 6, 1, 2, 1 } };



 int indexOfMaxSum = 0;


 int maxSum = 0;



 for ( int i = 0; i < arr.length; i++ ) {


 int[] innerArr = arr[ i ]; // grab inner array


 int sum = 0; // start sum at 0


 for ( int j : innerArr ) {


 // iterate over each int in array


 sum += j; // add each int to sum


 }


 if ( sum > maxSum ) {


 // if this sum is greater than the old max, store it


 maxSum = sum;


 indexOfMaxSum = i;


 }


 }



 System.out.println( String.format("Index %d has the highest sum with a sum of %d", indexOfMaxSum, maxSum ) );


 int [] arrayWithLargestSum = arr[indexOfMaxSum]; // return me



}



原作者:
...