Advertisement
Guest User

03

a guest
Jan 18th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _2x2
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. int[] dimentions = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
  11. int rows = dimentions[0];
  12. int cols = dimentions[1];
  13. int[,] matrix = new int[rows, cols];
  14. Initialized(matrix);
  15. int maxSum = int.MinValue;
  16. int targetRow = 0;
  17. int targetCol = 0;
  18. for (int row = 0; row < matrix.GetLength(0) - 2; row++)
  19. {
  20. for (int col = 0; col < matrix.GetLength(1) - 2; col++)
  21. {
  22. int currentSum = matrix[row, col] + matrix[row, col + 1] + matrix[row, col + 2] +
  23. matrix[row + 1, col] + matrix[row + 1, col + 1] + matrix[row + 1, col + 2] +
  24. matrix[row + 2, col] + matrix[row + 2, col + 1] + matrix[row + 2, col + 2];
  25.  
  26. if(maxSum < currentSum)
  27. {
  28. maxSum = currentSum;
  29. targetRow = row;
  30. targetCol = col;
  31. }
  32. }
  33. }
  34.  
  35. Console.WriteLine($"Sum = {maxSum}");
  36.  
  37. for (int row = targetRow; row <= targetRow + 2; row++)
  38. {
  39. for (int col = targetCol; col <= targetCol + 2; col++)
  40. {
  41. Console.Write(matrix[row,col] + " ");
  42. }
  43. Console.WriteLine();
  44. }
  45.  
  46. }
  47. private static void Initialized(int[,] matrix)
  48. {
  49. for (int row = 0; row < matrix.GetLength(0); row++)
  50. {
  51. int[] rowAsChar = Console.ReadLine().Split(" ",StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  52. for (int col = 0; col < matrix.GetLength(1); col++)
  53. {
  54. matrix[row, col] = rowAsChar[col];
  55. }
  56.  
  57. }
  58. }
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement