Advertisement
damdim

Untitled

May 30th, 2020
928
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class MaximalSum {
  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);
  6.  
  7.         String[] input = scan.nextLine().split("\\s+");
  8.         int rows = Integer.parseInt(input[0]);
  9.         int cols = Integer.parseInt(input[1]);
  10.         int[][] matrix = new int[rows][cols];
  11.        
  12.        
  13.         //Entering the matrix
  14.         for (int i = 0; i < rows; i++) {
  15.             String[] middle = scan.nextLine().split("\\s+");
  16.             for (int j = 0; j < cols; j++) {
  17.                 matrix[i][j] = Integer.parseInt(middle[j]);
  18.             }
  19.         }
  20.        
  21.         //Count of matrices 3x3 in the big matrix
  22.         int matrices = (rows - 2) * (cols - 2), counter = 0;
  23.         int sum = 0;
  24.         int maxSum = 0;
  25.         int startI = 0, startJ = 0;
  26.  
  27.         while (counter<matrices) {
  28.             //start and end indexes to go around all of the 3x3 matrices
  29.             int endI = startI + 3;
  30.             int endJ = startJ + 3;
  31.            
  32.            
  33.             for (int i = startI; i < endI; i++) {
  34.                 for (int j = startJ; j < endJ; j++) {
  35.                     sum += matrix[i][j];
  36.                 }
  37.             }
  38.            
  39.             if (sum > maxSum) {
  40.                 maxSum = sum;
  41.             }
  42.            
  43.             sum = 0;
  44.             counter++;
  45.            
  46.             if (endJ == cols) {
  47.                 startI++;
  48.                 startJ = 0;
  49.             } else {
  50.                 startI = endI - 3;
  51.                 startJ ++;
  52.             }
  53.         }
  54.         counter = 0;
  55.         startI = 0;
  56.         startJ = 0;
  57.         sum = 0;
  58.  
  59.         System.out.println("Sum = " + maxSum);
  60.        
  61.        
  62.         //Printing the 3x3 matrix with the max sum
  63.         while (counter < matrices) {
  64.             int endI = startI + 3;
  65.             int endJ = startJ + 3;
  66.             for (int i = startI; i < endI; i++) {
  67.                 for (int j = startJ; j < endJ; j++) {
  68.                     sum += matrix[i][j];
  69.                 }
  70.             }
  71.             if (sum == maxSum) {
  72.                 for (int i = startI; i < endI; i++) {
  73.                     for (int j = startJ; j < endJ; j++) {
  74.                         System.out.print(matrix[i][j] + " ");
  75.                     }
  76.                     System.out.println();
  77.                 }
  78.             }
  79.             sum = 0;
  80.             counter++;
  81.             if (endJ == cols) {
  82.                 startI++;
  83.                 startJ = 0;
  84.             } else {
  85.                 startI = endI - 3;
  86.                 startJ ++;
  87.             }
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement