Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace _3._Maximal_Sum
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. var input = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  12. var rows = input[0];
  13. var cols = input[1];
  14.  
  15. var matrix = ReadIntegerMatrix(rows, cols);
  16.  
  17. int maxSum = 0;
  18. int bestRowIndex = 0;
  19. int bestColIndex = 0;
  20.  
  21. for (int row = 0; row <= rows - 3; row++)
  22. {
  23. for (int col = 0; col <= cols - 3; col++)
  24. {
  25. int rowFirstSum = matrix[row, col] + matrix[row, col + 1] + matrix[row, col + 2];
  26. int rowSecondSum = matrix[row + 1, col] + matrix[row + 1, col + 1] + matrix[row + 1, col + 2];
  27. int rowThirdSum = matrix[row + 2, col] + matrix[row + 2, col + 1] + matrix[row + 2, col + 2];
  28.  
  29. int currentSum = rowFirstSum + rowSecondSum + rowThirdSum;
  30.  
  31. if (currentSum > maxSum)
  32. {
  33. maxSum = currentSum;
  34. bestRowIndex = row;
  35. bestColIndex = col;
  36. }
  37. }
  38. }
  39. Console.WriteLine($"Sum = {maxSum}");
  40.  
  41. for (int i = bestRowIndex; i <= bestRowIndex + 2; i++)
  42. {
  43. for (int j = bestColIndex; j <= bestColIndex + 2; j++)
  44. {
  45. Console.Write(matrix[i,j] + " ");
  46. }
  47. Console.WriteLine();
  48. }
  49. }
  50. private static int[,] ReadIntegerMatrix(int rows, int cols)
  51. {
  52. var matrix = new int[rows, cols];
  53.  
  54. for (int row = 0; row < rows; row++) // pro4itame matricata
  55. {
  56. var rowValues = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray(); // stoinostite na matricata
  57.  
  58. for (int col = 0; col < cols; col++)
  59. {
  60. matrix[row, col] = rowValues[col]; // sled pro4itane na redovete na ideksa pulnim kolonite
  61. }
  62. }
  63. return matrix;
  64. }
  65.  
  66. private static char[,] ReadCharMatrix(int rows, int cols)
  67. {
  68. char[,] matrix = new char[rows, cols];
  69.  
  70. for (int row = 0; row < rows; row++)
  71. {
  72. char[] rowValues = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(char.Parse).ToArray();
  73.  
  74. for (int col = 0; col < cols; col++)
  75. {
  76. matrix[row, col] = rowValues[col];
  77. }
  78. Console.WriteLine(); // za noviq red
  79. }
  80. return matrix;
  81. }
  82. private static void PrintMatrix (int[,] matrix)
  83. {
  84. for (int row = 0; row < matrix.GetLength(0); row++)
  85. {
  86. for (int col = 0; col < matrix.GetLength(1); col++)
  87. {
  88. Console.Write(matrix[row, col] + " ");
  89. }
  90. Console.WriteLine();
  91. }
  92. }
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement