marekov

MatrixMaxSum

Jun 30th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.71 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace MatrixMaxSum
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int size = int.Parse(Console.ReadLine());
  11.             int[,] matrix = FillMatrix(size);
  12.  
  13.             int[] coordinates = Console.ReadLine().
  14.                 Split().
  15.                 Select(int.Parse).
  16.                 ToArray();
  17.  
  18.             int maxSum = int.MinValue;
  19.             int currSum = 0;
  20.             int counter = 0;
  21.  
  22.             for (int i = 0; i < coordinates.Length; i += 2)
  23.             {
  24.                 int[] currCoordinates = coordinates.Skip(i)
  25.                     .Take(2)
  26.                     .ToArray();
  27.                 int currRow = currCoordinates[0];
  28.                 int currCol = currCoordinates[1];
  29.  
  30.                 while (currRow > 0)
  31.                 {
  32.                     for (int j = currRow; j <= currRow; j++)
  33.                     {
  34.                         for (int k = 0; k <= currCol; k++)
  35.                         {
  36.                             currSum += matrix[j,k];
  37.                         }
  38.                     }
  39.                     currRow--;
  40.                 }
  41.             }
  42.         }
  43.         public static int[,] FillMatrix(int size)
  44.         {
  45.             int[,] matrix = new int[size, size];
  46.  
  47.             for (int row = 0; row < size; row++)
  48.             {
  49.                 int[] currLine = Console.ReadLine()
  50.                     .Split()
  51.                     .Select(int.Parse)
  52.                     .ToArray();
  53.  
  54.                 for (int col = 0; col < size; col++)
  55.                 {
  56.                     matrix[row, col] = currLine[col];
  57.                 }
  58.             }
  59.             return matrix;
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment