Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class ex15_MaxPlatform3x3 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int rows = sc.nextInt();
- int cols = sc.nextInt();
- long sum = 0;
- long tempSum;
- int coordinateRow = 0;
- int coordinateCol = 0;
- sc.nextLine();
- long[][] matrix = new long[rows][];
- for (int i = 0; i < rows; i++) {
- long[] arr = Arrays.asList(sc.nextLine().split(" ")).stream().mapToLong(Long::parseLong).toArray();
- matrix[i] = arr;
- }
- for (int r = 0; r < rows - 2; r++) {
- for (int c = 0; c < cols - 2; c++) {
- tempSum = check3x3(matrix, r, c);
- if (tempSum > sum){
- sum = tempSum;
- coordinateRow = r;
- coordinateCol = c;
- }
- }
- }
- System.out.println(sum);
- for (int r = coordinateRow; r < 3 + coordinateRow; r++) {
- for (int c = coordinateCol; c < 3 + coordinateCol; c++) {
- System.out.print(matrix[r][c] + " ");
- }
- System.out.println();
- }
- }
- private static long check3x3(long[][] matrix, int r, int c) {
- long sum = 0;
- for (int r1 = r; r1 < 3 + r; r1++) {
- for (int c1 = c; c1 < 3 + c; c1++) {
- sum += matrix[r1][c1];
- }
- }
- return sum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement