Venciity

[ Telerik C#] MultidimArrays-Demos 2. MaxPlatform

Feb 26th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.01 KB | None | 0 0
  1. using System;
  2.  
  3. class MaxPlatform
  4. {
  5.     static void Main()
  6.     {
  7.         // Declare and initialize the matrix
  8.         int[,] matrix =
  9.         {
  10.             {0, 2, 4, 0, 9, 5},
  11.             {7, 1, 3, 3, 2, 1},
  12.             {1, 3, 9, 8, 5, 6},
  13.             {4, 6, 7, 9, 1, 0},
  14.         };
  15.  
  16.         // Find the maximal sum platform of size 2 x 2
  17.         int bestSum = int.MinValue;
  18.         int bestRow = 0;
  19.         int bestCol = 0;
  20.         for (int row = 0; row < matrix.GetLength(0) - 1; row++)
  21.         {
  22.             for (int col = 0; col < matrix.GetLength(1) - 1; col++)
  23.             {
  24.                 int sum = matrix[row, col] + matrix[row, col+1] +
  25.                     matrix[row+1, col] + matrix[row+1, col+1];
  26.                 if (sum > bestSum)
  27.                 {
  28.                     bestSum = sum;
  29.                     bestRow = row;
  30.                     bestCol = col;
  31.                 }
  32.             }
  33.         }
  34.  
  35.         // Print the result
  36.         Console.WriteLine("The best platform is:");
  37.         Console.WriteLine("  {0} {1}",
  38.             matrix[bestRow, bestCol],
  39.             matrix[bestRow, bestCol+1]);
  40.         Console.WriteLine("  {0} {1}",
  41.             matrix[bestRow+1, bestCol],
  42.             matrix[bestRow+1, bestCol+1]);
  43.         Console.WriteLine("The maximal sum is: {0}", bestSum);
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment