Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Security.Cryptography.X509Certificates;
- namespace _3._Maximal_Sum
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] rectangularMatrixSizes = Console.ReadLine()
- .Split(" ", StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToArray();
- int rows = rectangularMatrixSizes[0];
- int cols = rectangularMatrixSizes[1];
- int[,] rectangularMatrix = ReadMatrix(rows, cols);
- int maxSum = Int32.MinValue;
- int bestRow = 0;
- int bestCol = 0;
- for (int row = 0; row < rows - 2; row++)
- {
- for (int col = 0; col < cols - 2; col++)
- {
- int firstRowSum = rectangularMatrix[row, col] + rectangularMatrix[row, col + 1] +
- rectangularMatrix[row, col + 2];
- int secondRowSum = rectangularMatrix[row + 1, col] +
- rectangularMatrix[row + 1, col + 1] + rectangularMatrix[row + 1, col + 2];
- int thirdRowSum = rectangularMatrix[row + 2, col] + rectangularMatrix[row + 2, col + 1] +
- rectangularMatrix[row + 2, col + 2];
- int currentSum = firstRowSum + secondRowSum + thirdRowSum;
- if (currentSum > maxSum)
- {
- maxSum = currentSum;
- bestRow = row;
- bestCol = col;
- }
- }
- }
- Console.WriteLine($"Sum = {maxSum}");
- for (int row = bestRow; row <= bestRow + 2; row++)
- {
- for (int col = bestCol; col <= bestCol + 2; col++)
- {
- Console.Write(rectangularMatrix[row, col] + " ");
- }
- Console.WriteLine();
- }
- }
- static int[,] ReadMatrix(int rows, int cols)
- {
- int[,] rectangularMatrix = new int[rows, cols];
- for (int rowIndex = 0; rowIndex < rows; rowIndex++)
- {
- int[] numbersToInput = Console.ReadLine()
- .Split(" ", StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToArray();
- for (int colIndex = 0; colIndex < cols; colIndex++)
- {
- rectangularMatrix[rowIndex, colIndex] = numbersToInput[colIndex];
- }
- }
- return rectangularMatrix;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment