Advertisement
GeorgiGG

Untitled

Jan 24th, 2022
1,007
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _03.SumMatrix_2
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int[] dims = Console.ReadLine().Split().Select(int.Parse).ToArray();
  11.             int rows = dims[0];
  12.             int cols = dims[1];
  13.             int[,] matrix = ReadIntMatrix(rows, cols);
  14.             int[,] subMatrix = new int[3, 3];
  15.  
  16.             int currentSum = 0;
  17.             int maxRow = 0;
  18.             int maxCol = 0;
  19.             int maxSum = int.MinValue;
  20.  
  21.             for (int row = 0; row < rows - 2; row++)
  22.             {
  23.                 for (int col = 0; col < cols - 2; col++)
  24.                 {
  25.                     for (int i = 0; i < 3; i++)
  26.                     {
  27.                         for (int j = 0; j < 3; j++)
  28.                         {
  29.                             subMatrix[i, j] = matrix[row + i, col + j];
  30.                         }
  31.  
  32.                     }
  33.                     currentSum = MatrixSum(subMatrix);
  34.                     if (currentSum > maxSum)
  35.                     {
  36.                         maxSum = currentSum;
  37.                         maxRow = row;
  38.                         maxCol = col;
  39.                     }
  40.                 }
  41.             }
  42.             Console.WriteLine($"Sum = {maxSum}");
  43.             PrintIntMatrix(matrix, maxRow, maxCol, 3);
  44.         }
  45.         static int[,] ReadIntMatrix(int rows, int cols)
  46.         {
  47.             int[,] intMatrix = new int[rows, cols];
  48.  
  49.             for (int row = 0; row < rows; row++)
  50.             {
  51.                 int[] rowInput = Console.ReadLine().Split().Select(int.Parse).ToArray();
  52.  
  53.                 for (int col = 0; col < cols; col++)
  54.                 {
  55.                     intMatrix[row, col] = rowInput[col];
  56.                 }
  57.             }
  58.             return intMatrix;
  59.         }
  60.  
  61.         static void PrintIntMatrix(int[,] matrix, int startRow, int startCol, int size)
  62.         {
  63.             for (int row = startRow; row < startRow + size; row++)
  64.             {
  65.                 for (int col = startCol; col < startCol + size; col++)
  66.                 {
  67.                     Console.Write($"{matrix[row, col]} ");
  68.                 }
  69.                 Console.WriteLine();
  70.             }
  71.         }
  72.         static int MatrixSum(int[,] matrix)
  73.         {
  74.             int Sum = 0;
  75.             for (int row = 0; row < matrix.GetLength(0); row++)
  76.             {
  77.                 for (int col = 0; col < matrix.GetLength(1); col++)
  78.                 {
  79.                     Sum += matrix[row, col];
  80.                 }
  81.             }
  82.             return Sum;
  83.         }
  84.     }
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement