Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.13 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4.  
  5. namespace _2._Maximal_Sum
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             var rowsAndCols = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
  12.  
  13.             var row = rowsAndCols[0];
  14.  
  15.             var col = rowsAndCols[1];
  16.  
  17.             int maxSum = int.MinValue;
  18.  
  19.             int[,] matrix = new int[row, col];
  20.  
  21.             for (int rows = 0; rows < row; rows++)
  22.             {
  23.                 var inputsMatrix = Console.ReadLine().Split(" ").Select(int.Parse).ToArray();
  24.  
  25.                 for (int cols = 0; cols < col; cols++)
  26.                 {
  27.                     matrix[rows, cols] += inputsMatrix[cols];
  28.                 }
  29.             }
  30.  
  31.             int rowsLenght = matrix.GetLength(0) - 2;
  32.             int colLenght = matrix.GetLength(1) - 2;
  33.             int maxSumRow = -2;
  34.             int maxSumCol = -2;
  35.  
  36.             for (int rows= 0; rows < rowsLenght; rows++)
  37.             {
  38.                 for (int cols = 0; cols < colLenght; cols++)
  39.                 {
  40.                     var currentMaxSum = matrix[rows, cols] + matrix[rows, cols + 1] + matrix[rows, cols + 2] +
  41.                         matrix[rows + 1, cols] + matrix[rows + 1, cols + 1] + matrix[rows + 1, cols + 2]+
  42.                         matrix[rows + 2, cols] + matrix[rows + 2, cols + 1] + matrix[rows + 2, cols + 2];
  43.  
  44.                     if (currentMaxSum > maxSum)
  45.                     {
  46.                         maxSum = currentMaxSum;
  47.                         maxSumRow = rows;
  48.                         maxSumCol = cols;
  49.                     }
  50.                 }
  51.             }
  52.             Console.WriteLine($"Sum = {maxSum}");
  53.             Console.WriteLine($"{matrix[maxSumRow, maxSumCol]} {matrix[maxSumRow, maxSumCol + 1]} {matrix[maxSumRow, maxSumCol + 2]}");
  54.             Console.WriteLine($"{matrix[maxSumRow + 1, maxSumCol]} {matrix[maxSumRow + 1, maxSumCol + 1]} {matrix[maxSumRow + 1, maxSumCol + 2]}");
  55.             Console.WriteLine($"{matrix[maxSumRow + 2, maxSumCol]} {matrix[maxSumRow + 2, maxSumCol + 1]} {matrix[maxSumRow + 2, maxSumCol + 2]}");
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement