Advertisement
dimipan80

Maximal Sum

May 9th, 2015
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.21 KB | None | 0 0
  1. /* Write a program that reads a rectangular integer matrix of size N x M and finds in it the square 3 x 3 that has maximal sum of its elements.
  2.  * On the first line, you will receive the rows N and columns M. On the next N lines you will receive each row with its columns.
  3.  * Print the elements of the 3 x 3 square as a matrix, along with their sum. */
  4.  
  5. namespace _02.MaximalSum
  6. {
  7.     using System;
  8.     using System.Linq;
  9.  
  10.     class MaximalSum
  11.     {
  12.         private static int[,] _matrix;
  13.  
  14.         static void Main(string[] args)
  15.         {
  16.             Console.WriteLine("Enter two Positive Integer numbers on single line each, separated by a space, for sizes of your matrix: ");
  17.             int[] sizes = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  18.  
  19.             if (sizes.Length < 2 || sizes[0] < 3 || sizes[1] < 3)
  20.             {
  21.                 Console.WriteLine("Error!!! - Invalid matrix sizes!");
  22.                 return;
  23.             }
  24.  
  25.             _matrix = new int[sizes[0], sizes[1]];
  26.             Console.WriteLine("Enter your matrix content, each row on single line, each number, separated by a space: ");
  27.             for (int row = 0; row < _matrix.GetLength(0); row++)
  28.             {
  29.                 string[] numStr = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  30.                 if (numStr.Length != _matrix.GetLength(1))
  31.                 {
  32.                     Console.WriteLine("Error!!! - Invalid input!");
  33.                     return;
  34.                 }
  35.  
  36.                 for (int col = 0; col < _matrix.GetLength(1); col++)
  37.                 {
  38.                     _matrix[row, col] = int.Parse(numStr[col]);
  39.                 }
  40.             }
  41.  
  42.             int[] maxSumSquare = FindMaximalSumSquare3x3InMatrix();
  43.             PrintResults(maxSumSquare);
  44.         }
  45.  
  46.         private static int[] FindMaximalSumSquare3x3InMatrix()
  47.         {
  48.             int[] maxSums = new[] { int.MinValue, 0, 0 };
  49.             for (int row = 0; row + 2 < _matrix.GetLength(0); row++)
  50.             {
  51.                 for (int col = 0; col + 2 < _matrix.GetLength(1); col++)
  52.                 {
  53.                     int sum = _matrix[row, col] + _matrix[row, col + 1] + _matrix[row, col + 2] + _matrix[row + 1, col] + _matrix[row + 1, col + 1] + _matrix[row + 1, col + 2] + _matrix[row + 2, col] + _matrix[row + 2, col + 1] + _matrix[row + 2, col + 2];
  54.                     if (maxSums[0] < sum)
  55.                     {
  56.                         maxSums[0] = sum;
  57.                         maxSums[1] = row;
  58.                         maxSums[2] = col;
  59.                     }
  60.                 }
  61.             }
  62.  
  63.             return maxSums;
  64.         }
  65.  
  66.         private static void PrintResults(int[] results)
  67.         {
  68.             Console.WriteLine();
  69.             Console.WriteLine("Sum = {0}", results[0]);
  70.             Console.WriteLine();
  71.             for (int row = results[1]; row < results[1] + 3; row++)
  72.             {
  73.                 for (int col = results[2]; col < results[2] + 3; col++)
  74.                 {
  75.                     Console.Write("{0,3} ", _matrix[row, col]);
  76.                 }
  77.                 Console.WriteLine();
  78.             }
  79.             Console.WriteLine();
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement