Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- class MaximalSum
- {
- static void Main()
- {
- int[] matrixSize = Console.ReadLine().Split().Select(int.Parse).ToArray();
- int[,] matrix = new int[matrixSize[0], matrixSize[1]];
- //Filling the matrix
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- string[] numbers = Console.ReadLine().Split();
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- matrix[row, col] = int.Parse(numbers[col]);
- }
- }
- int numbersSum = 0;
- int bestSum = int.MinValue;
- int[,] numbersLocations = new int[3, 3];
- //Finding best 3x3 sum
- for (int row = 0; row < matrix.GetLength(0) - 2; row++)
- {
- for (int col = 0; col < matrix.GetLength(1) - 2; col++)
- {
- numbersSum = matrix[row, col] + matrix[row, col + 1] + matrix[row, col + 2] +
- matrix[row + 1, col] + matrix[row + 1, col + 1] + matrix[row + 1, col + 2] +
- matrix[row + 2, col] + matrix[row + 2, col + 1] + matrix[row + 2, col + 2];
- if (bestSum < numbersSum)
- {
- bestSum = numbersSum;
- for (int i = 0; i < numbersLocations.GetLength(0); i++)
- {
- for (int k = 0; k < numbersLocations.GetLength(1); k++)
- {
- numbersLocations[i, k] = matrix[row + i, col + k];
- }
- }
- }
- }
- }
- Console.WriteLine(Environment.NewLine + "Sum = {0}", bestSum);
- //printing bestSum
- for (int row = 0; row < numbersLocations.GetLength(0); row++)
- {
- for (int col = 0; col < numbersLocations.GetLength(1); col++)
- {
- Console.Write("{0} ", numbersLocations[row, col].ToString().PadLeft(3));
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement