Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.ComponentModel;
- using System.Linq;
- using System.Runtime.CompilerServices;
- namespace _5._Square_with_Maximum_Sum
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] matrixSizes = ReadFromConsole();
- int rows = matrixSizes[0];
- int cols = matrixSizes[1];
- int[,] matrix = new int[rows, cols];
- FillMatrix(matrix, rows, cols);
- int maxSum = Int32.MinValue;
- int maxRow = 0;
- int maxCol = 0;
- for (int rowIndex = 0; rowIndex < rows - 1; rowIndex++)
- {
- for (int colIndex = 0; colIndex < cols - 1; colIndex++)
- {
- int currentResult = 0;
- currentResult = (matrix[rowIndex, colIndex] + matrix[rowIndex, colIndex + 1])
- + (matrix[rowIndex + 1, colIndex] + matrix[rowIndex + 1, colIndex + 1]);
- if (currentResult > maxSum)
- {
- maxSum = currentResult;
- maxRow = rowIndex;
- maxCol = colIndex;
- }
- }
- }
- Console.WriteLine($"{matrix[maxRow, maxCol]} {matrix[maxRow, maxCol + 1]}");
- Console.WriteLine($"{matrix[maxRow + 1, maxCol]} {matrix[maxRow + 1, maxCol + 1]}");
- Console.WriteLine(maxSum);
- }
- static void FillMatrix(int[,] matrix, int rows, int cols)
- {
- for (int row = 0; row < rows; row++)
- {
- int[] numbersToFill = ReadFromConsole();
- for (int col = 0; col < cols; col++)
- {
- matrix[row, col] = numbersToFill[col];
- }
- }
- }
- public static int[] ReadFromConsole()
- {
- int[] matrixSizes = Console.ReadLine()
- .Split(", ", StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToArray();
- return matrixSizes;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment