TheBulgarianWolf

MaxPlatform2x2

Dec 16th, 2019
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.54 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace arraysExample
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             //Declare and initialize the matrix
  14.             int[,] matrix =
  15.             {
  16.                 {2,3,5,49,2},
  17.                 {5,2,1,4,5},
  18.                 {17,2,3,4,5},
  19.                 {4,8,9,6,3}
  20.             };
  21.  
  22.             //find the maximum sum platform of size 2x2
  23.             int bestSum = int.MinValue;
  24.             int bestRow = 0;
  25.             int bestCol = 0;
  26.            
  27.             for(int row = 0;row < matrix.GetLength(0)-1; row++)
  28.             {
  29.                 for(int col = 0; col< matrix.GetLength(1)-1; col++)
  30.                 {
  31.                     int sum = matrix[row, col] + matrix[row, col + 1] + matrix[row + 1, col] + matrix[row + 1, col + 1];
  32.                     if(sum > bestSum)
  33.                     {
  34.                         bestSum = sum;
  35.                         bestRow = row;
  36.                         bestCol = col;
  37.                     }
  38.                 }
  39.             }
  40.  
  41.             //print the result
  42.             Console.WriteLine("The best platform is: ");
  43.             Console.WriteLine("   {0} {1}", matrix[bestRow,bestCol], matrix[bestRow,bestCol+1]);
  44.             Console.WriteLine("   {0} {1}", matrix[bestRow+1,bestCol], matrix[bestRow+1,bestCol+1]);
  45.             Console.WriteLine("The maximum sum is : {0}", bestSum);
  46.             Console.ReadLine();
  47.         }
  48.     }
  49. }
Add Comment
Please, Sign In to add comment