yanchevilian

3.Maximal Sum/ C# Advanced

Jul 19th, 2021 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.76 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Security.Cryptography.X509Certificates;
  4.  
  5. namespace _3._Maximal_Sum
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int[] rectangularMatrixSizes = Console.ReadLine()
  12.                 .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  13.                 .Select(int.Parse)
  14.                 .ToArray();
  15.  
  16.             int rows = rectangularMatrixSizes[0];
  17.             int cols = rectangularMatrixSizes[1];
  18.  
  19.             int[,] rectangularMatrix = ReadMatrix(rows, cols);
  20.  
  21.             int maxSum = Int32.MinValue;
  22.             int bestRow = 0;
  23.             int bestCol = 0;
  24.  
  25.             for (int row = 0; row < rows - 2; row++)
  26.             {
  27.                 for (int col = 0; col < cols - 2; col++)
  28.                 {
  29.                     int firstRowSum = rectangularMatrix[row, col] + rectangularMatrix[row, col + 1] +
  30.                                       rectangularMatrix[row, col + 2];
  31.                     int secondRowSum = rectangularMatrix[row + 1, col] +
  32.                                        rectangularMatrix[row + 1, col + 1] + rectangularMatrix[row + 1, col + 2];
  33.                     int thirdRowSum = rectangularMatrix[row + 2, col] + rectangularMatrix[row + 2, col + 1] +
  34.                                       rectangularMatrix[row + 2, col + 2];
  35.                     int currentSum = firstRowSum + secondRowSum + thirdRowSum;
  36.  
  37.                     if (currentSum > maxSum)
  38.                     {
  39.                         maxSum = currentSum;
  40.                         bestRow = row;
  41.                         bestCol = col;
  42.                     }
  43.                 }
  44.             }
  45.  
  46.             Console.WriteLine($"Sum = {maxSum}");
  47.             for (int row = bestRow; row <= bestRow + 2; row++)
  48.             {
  49.                 for (int col = bestCol; col <=  bestCol + 2; col++)
  50.                 {
  51.                     Console.Write(rectangularMatrix[row, col] + " ");
  52.                 }
  53.                 Console.WriteLine();
  54.             }
  55.         }
  56.  
  57.         static int[,] ReadMatrix(int rows, int cols)
  58.             {
  59.                 int[,] rectangularMatrix = new int[rows, cols];
  60.                 for (int rowIndex = 0; rowIndex < rows; rowIndex++)
  61.                 {
  62.                     int[] numbersToInput = Console.ReadLine()
  63.                         .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  64.                         .Select(int.Parse)
  65.                         .ToArray();
  66.                     for (int colIndex = 0; colIndex < cols; colIndex++)
  67.                     {
  68.                         rectangularMatrix[rowIndex, colIndex] = numbersToInput[colIndex];
  69.                     }
  70.                 }
  71.  
  72.                 return rectangularMatrix;
  73.             }
  74.         }
  75.     }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment