Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- namespace _03.SumMatrix_2
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- int[] dims = Console.ReadLine().Split().Select(int.Parse).ToArray();
- int rows = dims[0];
- int cols = dims[1];
- int[,] matrix = ReadIntMatrix(rows, cols);
- int[,] subMatrix = new int[3, 3];
- int currentSum = 0;
- int maxRow = 0;
- int maxCol = 0;
- int maxSum = int.MinValue;
- for (int row = 0; row < rows - 2; row++)
- {
- for (int col = 0; col < cols - 2; col++)
- {
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; j < 3; j++)
- {
- subMatrix[i, j] = matrix[row + i, col + j];
- }
- }
- currentSum = MatrixSum(subMatrix);
- if (currentSum > maxSum)
- {
- maxSum = currentSum;
- maxRow = row;
- maxCol = col;
- }
- }
- }
- Console.WriteLine($"Sum = {maxSum}");
- PrintIntMatrix(matrix, maxRow, maxCol, 3);
- }
- static int[,] ReadIntMatrix(int rows, int cols)
- {
- int[,] intMatrix = new int[rows, cols];
- for (int row = 0; row < rows; row++)
- {
- int[] rowInput = Console.ReadLine().Split().Select(int.Parse).ToArray();
- for (int col = 0; col < cols; col++)
- {
- intMatrix[row, col] = rowInput[col];
- }
- }
- return intMatrix;
- }
- static void PrintIntMatrix(int[,] matrix, int startRow, int startCol, int size)
- {
- for (int row = startRow; row < startRow + size; row++)
- {
- for (int col = startCol; col < startCol + size; col++)
- {
- Console.Write($"{matrix[row, col]} ");
- }
- Console.WriteLine();
- }
- }
- static int MatrixSum(int[,] matrix)
- {
- int Sum = 0;
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- Sum += matrix[row, col];
- }
- }
- return Sum;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement