Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Collections.Generic;
- namespace _3._Maximal_Sum
- {
- class Program
- {
- static void Main(string[] args)
- {
- var input = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
- var rows = input[0];
- var cols = input[1];
- var matrix = ReadIntegerMatrix(rows, cols);
- int maxSum = 0;
- int bestRowIndex = 0;
- int bestColIndex = 0;
- for (int row = 0; row <= rows - 3; row++)
- {
- for (int col = 0; col <= cols - 3; col++)
- {
- int rowFirstSum = matrix[row, col] + matrix[row, col + 1] + matrix[row, col + 2];
- int rowSecondSum = matrix[row + 1, col] + matrix[row + 1, col + 1] + matrix[row + 1, col + 2];
- int rowThirdSum = matrix[row + 2, col] + matrix[row + 2, col + 1] + matrix[row + 2, col + 2];
- int currentSum = rowFirstSum + rowSecondSum + rowThirdSum;
- if (currentSum > maxSum)
- {
- maxSum = currentSum;
- bestRowIndex = row;
- bestColIndex = col;
- }
- }
- }
- Console.WriteLine($"Sum = {maxSum}");
- for (int i = bestRowIndex; i <= bestRowIndex + 2; i++)
- {
- for (int j = bestColIndex; j <= bestColIndex + 2; j++)
- {
- Console.Write(matrix[i,j] + " ");
- }
- Console.WriteLine();
- }
- }
- private static int[,] ReadIntegerMatrix(int rows, int cols)
- {
- var matrix = new int[rows, cols];
- for (int row = 0; row < rows; row++) // pro4itame matricata
- {
- var rowValues = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray(); // stoinostite na matricata
- for (int col = 0; col < cols; col++)
- {
- matrix[row, col] = rowValues[col]; // sled pro4itane na redovete na ideksa pulnim kolonite
- }
- }
- return matrix;
- }
- private static char[,] ReadCharMatrix(int rows, int cols)
- {
- char[,] matrix = new char[rows, cols];
- for (int row = 0; row < rows; row++)
- {
- char[] rowValues = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(char.Parse).ToArray();
- for (int col = 0; col < cols; col++)
- {
- matrix[row, col] = rowValues[col];
- }
- Console.WriteLine(); // za noviq red
- }
- return matrix;
- }
- private static void PrintMatrix (int[,] matrix)
- {
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- Console.Write(matrix[row, col] + " ");
- }
- Console.WriteLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement