Advertisement
xellscream

MaxSumOfMNMatrix

Jan 25th, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 KB | None | 0 0
  1. using System;
  2.  
  3. class MaximalSumOfMandN
  4. {
  5.     static void Main()
  6.     {
  7.         int n, m, count = int.MinValue, maxCount = int.MinValue, one = 0, two = 0, three = 0, four = 0, five = 0, six = 0, seven = 0, eight = 0, nine = 0;
  8.         n = int.Parse(Console.ReadLine());
  9.         if (n < 3)
  10.         {
  11.             Console.WriteLine("The N value must be at least equal to 3!");
  12.         }
  13.         m = int.Parse(Console.ReadLine());
  14.         if (n < 3)
  15.         {
  16.             Console.WriteLine("The M value must be at least equal to 3!");
  17.         }
  18.         int[,] myMatrix = new int[n,m];
  19.  
  20.         for (int row = 0; row < n; row++)
  21.         {
  22.             for (int col = 0; col < m; col++)
  23.             {
  24.                 myMatrix[row, col] = int.Parse(Console.ReadLine());
  25.             }
  26.         }
  27.         for (int rowA = 0, rowB = 1, rowC = 2; rowC < n; rowA++, rowB++, rowC++)
  28.         {
  29.             for (int colA = 0, colB = 1, colC = 2; colC < m; colA++, colB++, colC++)
  30.             {
  31.                 count = myMatrix[rowA, colA] + myMatrix[rowA, colB] + myMatrix[rowA, colC] + myMatrix[rowB, colA] + myMatrix[rowB, colB] + myMatrix[rowB, colC]
  32.                     + myMatrix[rowC, colA] + myMatrix[rowC, colB] + myMatrix[rowC, colC];
  33.  
  34.                 if (count  > maxCount)
  35.                 {
  36.                     maxCount = count;
  37.                     one = myMatrix[rowA, colA];
  38.                     two = myMatrix[rowA, colB];
  39.                     three = myMatrix[rowA, colC];
  40.                     four = myMatrix[rowB, colA];
  41.                     five = myMatrix[rowB, colB];
  42.                     six = myMatrix[rowB, colC];
  43.                     seven = myMatrix[rowC, colA];
  44.                     eight = myMatrix[rowC, colB];
  45.                     nine = myMatrix[rowC, colC];
  46.                 }
  47.                 else
  48.                 {
  49.                     continue;
  50.                 }
  51.             }
  52.  
  53.         }
  54.  
  55.         for (int row = 0; row < n; row++)
  56.         {
  57.             for (int col = 0; col < m; col++)
  58.             {
  59.                 Console.Write(myMatrix[row, col] + " ");
  60.             }
  61.             Console.WriteLine();
  62.         }
  63.         Console.WriteLine("\n\r\n\r");
  64.         Console.WriteLine("{0}, {1}, {2} \n\r{3}, {4}, {5} \n\r{6}, {7}, {8} \n\r\n\rMax sum = {9}", one, two, three, four, five, six, seven, eight, nine, maxCount);
  65.         Console.WriteLine();    
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement