Advertisement
deyanmalinov

04. Maximal Sum

May 25th, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);
  6.         String[] matrSize = scan.nextLine().split(" ");
  7.         int rows = Integer.parseInt(matrSize[0]);
  8.         int cols = Integer.parseInt(matrSize[1]);
  9.         int[][] matrix = new int[rows][cols];
  10.         for (int row = 0; row < matrix.length; row++) {
  11.             String[] line= scan.nextLine().split(" ");
  12.             for (int col = 0; col < matrix[0].length; col++) {
  13.                 matrix[row][col]= Integer.parseInt(line[col]);
  14.             }
  15.         }
  16.         int maxSum = 0;
  17.         int rowMax = 0;
  18.         int colMax = 0;
  19.         for (int row = 0; row < rows-2; row++) {
  20.             for (int col = 0; col < cols-2; col++) {
  21.                 int colSum = matrix[row][col] + matrix[row][col+1] + matrix[row][col+2] +
  22.                              matrix[row+1][col] + matrix[row+1][col+1] + matrix[row+1][col+2] +
  23.                              matrix[row+2][col] + matrix[row+2][col+1] + matrix[row+2][col+2];
  24.                 if (colSum > maxSum){
  25.                     maxSum = colSum;
  26.                     rowMax = row;
  27.                     colMax = col;
  28.                 }
  29.             }
  30.         }
  31.         System.out.println("Sum = " + maxSum);
  32.         System.out.println(matrix[rowMax][colMax]+ " " + matrix[rowMax][colMax+1]+ " " + matrix[rowMax][colMax+2]);
  33.         System.out.println(matrix[rowMax+1][colMax]+ " " + matrix[rowMax+1][colMax+1]+ " " + matrix[rowMax+1][colMax+2]);
  34.         System.out.println(matrix[rowMax+2][colMax]+ " " + matrix[rowMax+2][colMax+1]+ " " + matrix[rowMax+2][colMax+2]);
  35.  
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement