Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class ex15_MaxPlatform3x3 {
  5. public static void main(String[] args) {
  6. Scanner sc = new Scanner(System.in);
  7. int rows = sc.nextInt();
  8. int cols = sc.nextInt();
  9. long sum = 0;
  10. long tempSum;
  11. int coordinateRow = 0;
  12. int coordinateCol = 0;
  13. sc.nextLine();
  14. long[][] matrix = new long[rows][];
  15. for (int i = 0; i < rows; i++) {
  16. long[] arr = Arrays.asList(sc.nextLine().split(" ")).stream().mapToLong(Long::parseLong).toArray();
  17. matrix[i] = arr;
  18. }
  19. for (int r = 0; r < rows - 2; r++) {
  20. for (int c = 0; c < cols - 2; c++) {
  21. tempSum = check3x3(matrix, r, c);
  22. if (tempSum > sum){
  23. sum = tempSum;
  24. coordinateRow = r;
  25. coordinateCol = c;
  26. }
  27. }
  28. }
  29. System.out.println(sum);
  30. for (int r = coordinateRow; r < 3 + coordinateRow; r++) {
  31. for (int c = coordinateCol; c < 3 + coordinateCol; c++) {
  32. System.out.print(matrix[r][c] + " ");
  33. }
  34. System.out.println();
  35. }
  36. }
  37.  
  38. private static long check3x3(long[][] matrix, int r, int c) {
  39. long sum = 0;
  40. for (int r1 = r; r1 < 3 + r; r1++) {
  41. for (int c1 = c; c1 < 3 + c; c1++) {
  42. sum += matrix[r1][c1];
  43. }
  44. }
  45. return sum;
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement