Advertisement
AlexVanchov

Matrix

Mar 30th, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.04 KB | None | 0 0
  1. namespace Matrix
  2. {
  3.     using System;
  4.     using System.Linq;
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Console.WriteLine("Input(row, col)");
  10.             var rowCol = Console.ReadLine()
  11.                    .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  12.                    .Select(int.Parse)
  13.                    .ToArray();
  14.             int[][] matrix = new int[rowCol[0]][];
  15.             int[][] maxMatrix = new int[3][];
  16.  
  17.             Console.WriteLine("Rows: "); // spaces
  18.             for (int row = 0; row < rowCol[0]; row++)
  19.             {
  20.                 matrix[row] = Console.ReadLine()
  21.                     .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  22.                     .Select(int.Parse)
  23.                     .ToArray();
  24.             }
  25.             for (int row = 0; row < 3; row++)
  26.             {
  27.                 maxMatrix[row] = matrix[row].Take(3).ToArray();
  28.             }
  29.             for (int row = 0; row < rowCol[0] - 2; row++)
  30.             {
  31.                 for (int col = 0; col < rowCol[1] - 2; col++)
  32.                 {
  33.                     if (matrix[row].Skip(col).Take(3).Sum() +
  34.                         matrix[row + 1].Skip(col).Take(3).Sum() +
  35.                         matrix[row + 2].Skip(col).Take(3).Sum()
  36.                         >
  37.                         maxMatrix[0].Sum() +
  38.                         maxMatrix[1].Sum() +
  39.                         maxMatrix[2].Sum())
  40.                     {
  41.                         maxMatrix[0] = matrix[row].Skip(col).Take(3).ToArray();
  42.                         maxMatrix[1] = matrix[row + 1].Skip(col).Take(3).ToArray();
  43.                         maxMatrix[2] = matrix[row + 2].Skip(col).Take(3).ToArray();
  44.                     }
  45.                 }
  46.             }
  47.             Console.WriteLine($"Sum = {maxMatrix[0].Sum() + maxMatrix[1].Sum() + maxMatrix[2].Sum()}");
  48.             for (int row = 0; row < 3; row++)
  49.             {
  50.                 Console.WriteLine(string.Join(" ", maxMatrix[row]));
  51.             }
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement