你的位置:首页 > Java教程

[Java教程]JAVA,求m*n阶矩阵中的2*2阶矩阵和的最大值


题目:输入一个m*n阶矩阵,找出m*n阶矩阵中的二阶矩阵和的最大值

输入样例:

1 2 3 0 4;4 3 5 1 2;3 2 4 7 5

输出样例:

17

------------------------------------------------I am a Dividing line---------------------------------------------

java代码:

import java.util.Scanner;/** * m*n阶矩阵中的2*2阶矩阵和最大值 * * @author ForeverLover * */public class Matrix {	public static void main(String[] args) {		Scanner scan = new Scanner(System.in);		int m;		int n;		String s = scan.nextLine();		String[] sa = s.split(";");		m = sa.length;		String[][] ss = new String[m][];		for (int i = 0; i < m; i++)			ss[i] = sa[i].split(" ");		n = ss[0].length;		int[][] array = new int[m][n];		for (int i = 0; i < m; i++) {			for (int j = 0; j < n; j++) {				array[i][j] = Integer.parseInt((ss[i][j]));			}		}		int max = 0;		for (int i = 0; i < m - 1; i++) {			int sum = 0;			for (int j = 0; j < n - 1; j++) {				sum = array[i][j] + array[i][j + 1] + array[i + 1][j]						+ array[i + 1][j + 1];				max = sum >= max ? sum : max;			}		}		System.out.println(max);	}}