实验思路:
通过键入二维数组的信息构建二维数组,求出数组所有二位子数组的和并求出最大值
实验代码:
package er_wei_shu_zu; import java.util.Scanner; public class rwsz { static Scanner scanner = new Scanner(System.in); public static void main(String args[]){ int m,n; int x; Scanner scanner = new Scanner(System.in); System.out.println("二维数组的列数:"); m = scanner.nextInt(); System.out.println("二维数组的行数:"); n = scanner.nextInt(); int arr[][] = new int[n][m]; System.out.println("请逐行输入二位数组:"); for(int i = 0;i<n;i++) for(int j=0;j<m;j++) { arr[i][j] = scanner.nextInt(); } System.out.println(" "); x = maxArrSum(arr); System.out.println("该二维数组最大子数组的和为 "+x); } //遍历所有二维数组的矩形区域 static int maxArrSum(int arr[][]){ int m = arr.length; int n = arr[0].length; int p[][] = arrSum(arr); int ans = Integer.MIN_VALUE; for(int i=1; i<=m; i++){ for(int j=1; j<=n; j++){ for(int endi=i; endi <=m; endi++){ for(int endj=j; endj<=n; endj++){ int sum = p[endi][endj] - p[i-1][endj] - p[endi][j-1] + p[i-1][j-1]; if(ans < sum) ans = sum; } } } } return ans; } public static int[][] arrSum(int arr[][]){ int m = arr.length; int n = arr[0].length; int p[][] = new int[m+1][n+1]; p[0][0] = arr[0][0]; for(int i=0; i<=m; i++) p[i][0] = 0; for(int i=0; i<=n; i++) p[0][i] = 0; for(int i=1; i<=m; i++){ for(int j=1; j<=n; j++){ p[i][j] = p[i-1][j] + p[i][j-1] + arr[i-1][j-1] - p[i-1][j-1]; } } return p; } }
实验截图: