Advertisement
striking

Untitled

Feb 13th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7.  
  8. class MaximalSum
  9. {
  10. static void Main()
  11. {
  12. int[] matrixSize = Console.ReadLine().Split().Select(int.Parse).ToArray();
  13. int[,] matrix = new int[matrixSize[0], matrixSize[1]];
  14.  
  15. //Filling the matrix
  16. for (int row = 0; row < matrix.GetLength(0); row++)
  17. {
  18. string[] numbers = Console.ReadLine().Split();
  19. for (int col = 0; col < matrix.GetLength(1); col++)
  20. {
  21. matrix[row, col] = int.Parse(numbers[col]);
  22. }
  23. }
  24.  
  25. int numbersSum = 0;
  26. int bestSum = int.MinValue;
  27. int[,] numbersLocations = new int[3, 3];
  28.  
  29. //Finding best 3x3 sum
  30. for (int row = 0; row < matrix.GetLength(0) - 2; row++)
  31. {
  32. for (int col = 0; col < matrix.GetLength(1) - 2; col++)
  33. {
  34. numbersSum = matrix[row, col] + matrix[row, col + 1] + matrix[row, col + 2] +
  35. matrix[row + 1, col] + matrix[row + 1, col + 1] + matrix[row + 1, col + 2] +
  36. matrix[row + 2, col] + matrix[row + 2, col + 1] + matrix[row + 2, col + 2];
  37.  
  38. if (bestSum < numbersSum)
  39. {
  40. bestSum = numbersSum;
  41. for (int i = 0; i < numbersLocations.GetLength(0); i++)
  42. {
  43. for (int k = 0; k < numbersLocations.GetLength(1); k++)
  44. {
  45. numbersLocations[i, k] = matrix[row + i, col + k];
  46. }
  47. }
  48. }
  49. }
  50. }
  51.  
  52. Console.WriteLine(Environment.NewLine + "Sum = {0}", bestSum);
  53. //printing bestSum
  54. for (int row = 0; row < numbersLocations.GetLength(0); row++)
  55. {
  56. for (int col = 0; col < numbersLocations.GetLength(1); col++)
  57. {
  58. Console.Write("{0} ", numbersLocations[row, col].ToString().PadLeft(3));
  59. }
  60. Console.WriteLine();
  61. }
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement