package a.b.c; import java.util.*; /* 3 4 -3 5 -1 5 2 4 -2 4 -1 3 -1 3 */ public class Card { static int row; static int column; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); row = scanner.nextInt(); column = scanner.nextInt(); int[][] jz = new int[row][column]; for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { jz[i][j]=scanner.nextInt(); } } //计算所有的子矩阵和 找到最大值 int max=0; for (int i = 0; i <row; i++) { for (int j = 0; j <column; j++) { max=Math.max(findn(i,j,jz,max),findm(i,j,jz,max)); } } System.out.println(max); } /** * n m代表子矩阵的大小 * @param n * @param m * @param jz * @param max * @return */ public static int findn(int n ,int m,int[][] jz,int max){ int result = 0; System.out.println(n+" "+m); if(n<=row&&m<=column){ for (int i = n; i < row; i++) { for (int j = m; j < column; j++) { result=result+jz[i][j]; } } System.out.println(result); max = Math.max(result,max); findn(n+1,m,jz,max); findm(n,m+1,jz,max); } return max; } public static int findm(int n ,int m,int[][] jz,int max){ int result = 0; System.out.println(n+" "+m); if(n<=row&&m<=column){ for (int i = n; i < row; i++) { for (int j = m; j < column; j++) { result=result+jz[i][j]; } } System.out.println(result); max = Math.max(result,max); findm(n,m+1,jz,max); findn(n+1,m,jz,max); } return max; } }
计算给定矩阵的子矩阵最大的和
最新推荐文章于 2024-08-26 16:40:46 发布