Advertisement
Guest User

Untitled

a guest
Sep 19th, 2021
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace MaximalSum
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. int[] matrixSize = Console.ReadLine().Split().Select(int.Parse).ToArray();
  11.  
  12. int rowsLength = matrixSize[0];
  13. int colsLength = matrixSize[1];
  14.  
  15. int[,] matrix = new int[rowsLength, colsLength];
  16.  
  17. for (int row = 0; row < matrix.GetLength(0); row++)
  18. {
  19. int[] elements = Console.ReadLine().Split().Select(int.Parse).ToArray();
  20.  
  21. for (int col = 0; col < matrix.GetLength(1); col++)
  22. {
  23. matrix[row, col] = elements[col];
  24. }
  25. }
  26.  
  27. int maxSquare3x3Sum = int.MinValue;
  28. int rowMaxIndex = 0;
  29. int colMaxIndex = 0;
  30.  
  31. for (int row = 0; row < matrix.GetLength(0) - 3 + 1; row++)
  32. {
  33. for (int col = 0; col < matrix.GetLength(1) - 3 + 1; col++)
  34. {
  35. int currentSquareSum = matrix[row, col] + matrix[row, col + 1] + matrix[row, col + 2] + matrix[row + 1, col] +
  36. matrix[row + 1, col + 1] + matrix[row + 1, col + 2] + matrix[row + 2, col] + matrix[row + 2, col + 1] +
  37. matrix[row + 2, col + 2];
  38.  
  39. if (currentSquareSum > maxSquare3x3Sum)
  40. {
  41. maxSquare3x3Sum = currentSquareSum;
  42. rowMaxIndex = row;
  43. colMaxIndex = col;
  44. }
  45. }
  46. }
  47.  
  48. Console.WriteLine($"Sum = {maxSquare3x3Sum}");
  49. Console.WriteLine(matrix[rowMaxIndex, colMaxIndex] + " " + matrix[rowMaxIndex, colMaxIndex + 1] + " " + matrix[rowMaxIndex, colMaxIndex + 2]);
  50. Console.WriteLine(matrix[rowMaxIndex + 1, colMaxIndex] + " " + matrix[rowMaxIndex + 1, colMaxIndex + 1] + " " + matrix[rowMaxIndex + 1, colMaxIndex + 2]);
  51. Console.WriteLine(matrix[rowMaxIndex + 2, colMaxIndex] + " " + matrix[rowMaxIndex + 2, colMaxIndex + 1] + " " + matrix[rowMaxIndex + 2, colMaxIndex + 2]);
  52. }
  53. }
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement