Advertisement
Guest User

Untitled

a guest
May 17th, 2023
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.21 KB | None | 0 0
  1. using System;
  2.  
  3. namespace _3._Maximal_Sum
  4. {
  5.     internal class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int[] size = Console.ReadLine()
  10.                 .Split()
  11.                 .Select(int.Parse)
  12.                 .ToArray();
  13.  
  14.             int rows = size[0];
  15.             int cols = size[1];
  16.             int[,] matrix = new int[rows, cols];
  17.             int sum = int.MinValue;
  18.             int rowIndex = 0;
  19.             int colIndex = 0;
  20.  
  21.             for (int row = 0; row < rows; row++)
  22.             {
  23.                 int[] array = Console.ReadLine()
  24.                 .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) //here
  25.                 .Select(int.Parse)
  26.                 .ToArray();
  27.  
  28.                 for (int col = 0; col < cols; col++)
  29.                 {
  30.                     matrix[row, col] = array[col];
  31.                 }
  32.             }
  33.  
  34.             for (int row = 0; row < rows - 2; row++)
  35.             {
  36.                 for (int col = 0; col < cols - 2; col++)
  37.                 {
  38.                     int curentSum = 0;
  39.                     curentSum += matrix[row, col];
  40.                     curentSum += matrix[row, col + 1];
  41.                     curentSum += matrix[row, col + 2];
  42.                     curentSum += matrix[row + 1, col];
  43.                     curentSum += matrix[row + 1, col + 1];
  44.                     curentSum += matrix[row + 1, col + 2];
  45.                     curentSum += matrix[row + 2, col];
  46.                     curentSum += matrix[row + 2, col + 1];
  47.                     curentSum += matrix[row + 2, col + 2];
  48.                     if (curentSum > sum)
  49.                     {
  50.                         sum = curentSum;
  51.                         rowIndex = row;
  52.                         colIndex = col;
  53.                     }
  54.                 }
  55.  
  56.             }
  57.             Console.WriteLine($"Sum = {sum}");
  58.             for (int indexR = rowIndex; indexR < rowIndex + 3; indexR++)
  59.             {
  60.                 for (int indexC = colIndex; indexC < colIndex + 3; indexC++)
  61.                 {
  62.                     Console.Write(matrix[indexR, indexC] + " ");
  63.                 }
  64.                 Console.WriteLine();
  65.             }
  66.  
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement