问题描述
下面是一个 20x20 的矩阵,矩阵中的每个数字是一个1到9之间的数字,请注意显示时去除了分隔符号。
6985924183938786894117615876963131759284373473483266274834855367125655616786474316121686927432329479135474133499627734472797994592984882468753776983346838791379564934213653657177452192437929387261138293919353216243561277542961447639692577889623397251379473293381443494533129939975611718829888775934996121686889572134852255485345959294726896321249633182425549221359364719193427269656436895944919899246
矩阵中一个子矩阵的值是指子矩阵中所有数值的和。
请问,矩阵中值最大的一个5x5的子矩阵的值是多少?
答案提交
这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。
运行限制
·最大运行时间:1s
最大运行内存:256M
解题思路:暴力解题法
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改public class Main {public static void main(String[] args) {Scanner scan = new Scanner(System.in);//在此输入您的代码...String[] strs = { "69859241839387868941","17615876963131759284","37347348326627483485","53671256556167864743","16121686927432329479","13547413349962773447","27979945929848824687","53776983346838791379","56493421365365717745","21924379293872611382","93919353216243561277","54296144763969257788","96233972513794732933","81443494533129939975","61171882988877593499","61216868895721348522","55485345959294726896","32124963318242554922","13593647191934272696","56436895944919899246"};int ans = 0;for(int i = 0;i <= 15;i++){for(int j = 0;j <= 15;j++){int sum = 0;for(int k = i;k < i + 5;k++){for(int h = j;h < j + 5;h++){sum += (strs[k].charAt(h) - '0');}}ans = Math.max(ans,sum);}}System.out.println(ans);scan.close();}
}