Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- String[] matrSize = scan.nextLine().split(" ");
- int rows = Integer.parseInt(matrSize[0]);
- int cols = Integer.parseInt(matrSize[1]);
- int[][] matrix = new int[rows][cols];
- for (int row = 0; row < matrix.length; row++) {
- String[] line= scan.nextLine().split(" ");
- for (int col = 0; col < matrix[0].length; col++) {
- matrix[row][col]= Integer.parseInt(line[col]);
- }
- }
- int maxSum = 0;
- int rowMax = 0;
- int colMax = 0;
- for (int row = 0; row < rows-2; row++) {
- for (int col = 0; col < cols-2; col++) {
- int colSum = matrix[row][col] + matrix[row][col+1] + matrix[row][col+2] +
- matrix[row+1][col] + matrix[row+1][col+1] + matrix[row+1][col+2] +
- matrix[row+2][col] + matrix[row+2][col+1] + matrix[row+2][col+2];
- if (colSum > maxSum){
- maxSum = colSum;
- rowMax = row;
- colMax = col;
- }
- }
- }
- System.out.println("Sum = " + maxSum);
- System.out.println(matrix[rowMax][colMax]+ " " + matrix[rowMax][colMax+1]+ " " + matrix[rowMax][colMax+2]);
- System.out.println(matrix[rowMax+1][colMax]+ " " + matrix[rowMax+1][colMax+1]+ " " + matrix[rowMax+1][colMax+2]);
- System.out.println(matrix[rowMax+2][colMax]+ " " + matrix[rowMax+2][colMax+1]+ " " + matrix[rowMax+2][colMax+2]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement