Advertisement
gabi11

Multidimensional Arrays - 03. Maximal Sum

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