ellapt

T8.2.MaxSum3x3Platform

Jan 17th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. using System;
  2.  
  3. class MaxSum3x3Platform
  4. {
  5. static void Main()
  6. {
  7. /* int[,] matrix = {
  8. {7, 1, 3, 3, 2, 1},
  9. {1, 3, 9, 8, 5, 6},
  10. {4, 6, 7, 9, 1, 0} };
  11. */
  12. Console.WriteLine("Read rectangular matrix size N x M, find in it 3 x 3 square with max sum of its elements");
  13. Console.Write("Enter number of rows of the matrix to be printed: ");
  14. int rSize = int.Parse(Console.ReadLine());
  15. Console.Write("Enter number of columns of the matrix to be printed: ");
  16. int cSize = int.Parse(Console.ReadLine());
  17. int[,] matrix = new int[rSize, cSize];
  18. Console.Write("Enter the elements of the matrix to be printed: ");
  19. for (int i = 0; i < rSize; i++)
  20. {
  21. for (int j = 0; j < cSize; j++)
  22. {
  23. matrix[i,j]=int.Parse(Console.ReadLine());
  24. }
  25. }
  26.  
  27. int bestSum = int.MinValue;
  28. int rStart=0;
  29. int cStart=0;
  30. for (int row = 0; row < rSize - 2; row++)
  31. {
  32. for (int col = 0; col < cSize - 2; col++)
  33. {
  34. int sum = matrix[row, col] + matrix[row, col + 1] + matrix[row, col + 2] + matrix[row + 1, col]
  35. + matrix[row + 1, col + 1] + matrix[row + 1, col + 2] + matrix[row + 2, col]
  36. + matrix[row + 2, col + 1] + matrix[row + 2, col + 2];
  37. if (sum > bestSum)
  38. {
  39. bestSum = sum;
  40. rStart = row;
  41. cStart = col;
  42. }
  43. }
  44. }
  45. Console.WriteLine("The 3x3 platform with maximal sum={0} is:",bestSum);
  46. for (int row= rStart; row < rStart+3; row++)
  47. {
  48. for (int col= cStart; col < cStart+3; col++)
  49. {
  50. Console.Write("{0,4}",matrix[row,col]);
  51. }
  52. Console.WriteLine();
  53. }
  54.  
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment