Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace _2._Maximal_Sum
- {
- class Program
- {
- static void Main(string[] args)
- {
- var rowsAndCols = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
- var row = rowsAndCols[0];
- var col = rowsAndCols[1];
- int maxSum = int.MinValue;
- int[,] matrix = new int[row, col];
- for (int rows = 0; rows < row; rows++)
- {
- var inputsMatrix = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
- for (int cols = 0; cols < col; cols++)
- {
- matrix[rows, cols] += inputsMatrix[cols];
- }
- }
- int rowsLenght = matrix.GetLength(0) - 2;
- int colLenght = matrix.GetLength(1) - 2;
- int maxSumRow = -2;
- int maxSumCol = -2;
- for (int rows= 0; rows < rowsLenght; rows++)
- {
- for (int cols = 0; cols < colLenght; cols++)
- {
- var currentMaxSum = matrix[rows, cols] + matrix[rows, cols + 1] + matrix[rows, cols + 2] +
- matrix[rows + 1, cols] + matrix[rows + 1, cols + 1] + matrix[rows + 1, cols + 2]+
- matrix[rows + 2, cols] + matrix[rows + 2, cols + 1] + matrix[rows + 2, cols + 2];
- if (currentMaxSum > maxSum)
- {
- maxSum = currentMaxSum;
- maxSumRow = rows;
- maxSumCol = cols;
- }
- }
- }
- Console.WriteLine($"Sum = {maxSum}");
- Console.WriteLine($"{matrix[maxSumRow, maxSumCol]} {matrix[maxSumRow, maxSumCol + 1]} {matrix[maxSumRow, maxSumCol + 2]}");
- Console.WriteLine($"{matrix[maxSumRow + 1, maxSumCol]} {matrix[maxSumRow + 1, maxSumCol + 1]} {matrix[maxSumRow + 1, maxSumCol + 2]}");
- Console.WriteLine($"{matrix[maxSumRow + 2, maxSumCol]} {matrix[maxSumRow + 2, maxSumCol + 1]} {matrix[maxSumRow + 2, maxSumCol + 2]}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement