Advertisement
stanevplamen

02.2.2.MaxPlatformSum

May 26th, 2013
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.47 KB | None | 0 0
  1. using System;
  2.  
  3. class MaxPlatformSum
  4. {
  5.     static void Main()
  6.     {
  7.         Console.Write("Please enter the number of rows; r = ");
  8.         int rows = int.Parse(Console.ReadLine());      
  9.         Console.Write("Please enter the number of columns; c = ");
  10.         int columns = int.Parse(Console.ReadLine());
  11.         int matrixSize = rows * columns;
  12.         int[,] matrix = new int[rows, columns];
  13.         int counter = 0;
  14.         int bestSum = int.MinValue;
  15.         int bestRow = -1;
  16.         int bestCol = -1;
  17.  
  18.         // declare
  19.         for (int row = 0; row < matrix.GetLength(0); row++)
  20.         {
  21.             for (int col = 0; col < matrix.GetLength(1); col++)
  22.             {
  23.                 counter++;
  24.                 matrix[row, col] = counter;
  25.             }
  26.         }
  27.         Console.WriteLine("The matrix is: ");
  28.         PrintArray(matrix);
  29.  
  30.         // find
  31.         for (int row = 0; row < matrix.GetLength(0) - 2; row++)
  32.         {
  33.             for (int col = 0; col < matrix.GetLength(1) - 2; col++)
  34.             {
  35.                 int sum = matrix[row, col] + matrix[row, col + 1] + matrix[row, col + 2]
  36.                         + matrix[row + 1, col] + matrix[row + 1, col + 1] + matrix[row + 1, col + 2]
  37.                         + matrix[row + 2, col] + matrix[row + 2, col + 1] + matrix[row + 2, col + 2];
  38.                 if (sum > bestSum)
  39.                 {
  40.                     bestSum = sum;
  41.                     bestRow = row;
  42.                     bestCol = col;
  43.                 }
  44.             }
  45.         }
  46.         // print
  47.  
  48.         Console.WriteLine("The best platform is: ");
  49.         Console.WriteLine("   {0} {1} {2}", matrix[bestRow, bestCol], matrix[bestRow, bestCol + 1], matrix[bestRow, bestCol + 2]);
  50.         Console.WriteLine("   {0} {1} {2}", matrix[bestRow + 1, bestCol], matrix[bestRow + 1, bestCol + 1],   matrix[bestRow + 1, bestCol + 2]);
  51.         Console.WriteLine("   {0} {1} {2}", matrix[bestRow + 2, bestCol], matrix[bestRow + 2, bestCol + 1], matrix[bestRow + 2, bestCol + 2]);
  52.         Console.WriteLine();
  53.         Console.WriteLine("The maximal sum is: {0}", bestSum);
  54.  
  55.     }
  56.  
  57.     private static void PrintArray(int[,] matrix)
  58.     {
  59.         for (int row = 0; row < matrix.GetLength(0); row++)
  60.         {
  61.             for (int col = 0; col < matrix.GetLength(1); col++)
  62.             {
  63.                 Console.Write("{0,3} ", matrix[row, col]);
  64.             }
  65.             Console.WriteLine();
  66.         }
  67.         Console.WriteLine();
  68.     }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement