Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _04.MaximalSum
- {
- public class MaximalSum
- {
- public static void Main(string[] args)
- {
- var input = Console.ReadLine();
- int[] dimensions = input
- .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToArray();
- int[,] matrix = new int[dimensions[0], dimensions[1]];
- for (int rows = 0; rows < dimensions[0]; rows++)
- {
- var rowIncomes = Console.ReadLine()
- .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToArray();
- for (int cols = 0; cols < dimensions[1]; cols++)
- {
- matrix[rows, cols] = rowIncomes[cols];
- }
- }
- int maxSum = int.MinValue;
- int rowIndex = 0;
- int colIndex = 0;
- for (int rows = 0; rows < matrix.GetLength(0) - 2; rows++)
- {
- for (int cols = 0; cols < matrix.GetLength(1) - 2; cols++)
- {
- var currentSum = 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 (currentSum > maxSum)
- {
- maxSum = currentSum;
- rowIndex = rows;
- colIndex = cols;
- }
- }
- }
- Console.WriteLine($"Sum = {maxSum}");
- Console.WriteLine($"{matrix[rowIndex, colIndex]} {matrix[rowIndex, colIndex + 1]} {matrix[rowIndex, colIndex + 2]}");
- Console.WriteLine($"{matrix[rowIndex + 1, colIndex]} {matrix[rowIndex + 1, colIndex + 1]} {matrix[rowIndex + 1, colIndex + 2]}");
- Console.WriteLine($"{matrix[rowIndex + 2, colIndex]} {matrix[rowIndex + 2, colIndex + 1]} {matrix[rowIndex + 2, colIndex + 2]}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement