Advertisement
ScorpS

Maximal Sum From File

Jan 26th, 2013
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1.  
  2. //Write a program that reads a text file containing a square matrix of numbers and finds in the matrix
  3. //an area of size 2 x 2 with a maximal sum of its elements. The first line in the input file contains
  4. //the size of matrix N. Each of the next N lines contain N numbers separated by space. The output
  5. //should be a single number in a separate text file. Example:
  6. //4
  7. //2 3 3 4
  8. //0 2 3 4       --->    17
  9. //3 7 1 2
  10. //4 3 3 2
  11.  
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.IO;
  18.  
  19.  
  20. class MaximalSumFromFile5
  21. {
  22.  
  23.  
  24.     private static int FindMaxSum(int[,] matrix)
  25.     {
  26.  
  27.         int bestSum = int.MinValue;
  28.         int currentSum = 0;
  29.  
  30.         for (int row = 0; row < matrix.GetLength(0) - 1; row++)
  31.         {
  32.             for (int col = 0; col < matrix.GetLength(1) - 1; col++)
  33.             {
  34.                 currentSum = matrix[row, col] + matrix[row, col + 1] +
  35.                              matrix[row + 1, col] + matrix[row + 1, col + 1];
  36.                 if (bestSum < currentSum)
  37.                 {
  38.                     bestSum = currentSum;
  39.                 }
  40.                 currentSum = 0;
  41.             }
  42.         }
  43.         return bestSum;
  44.     }
  45.  
  46.     static void Main()
  47.     {
  48.         StreamReader read = new StreamReader("../../inputMatrix.txt");
  49.         StreamWriter write = new StreamWriter("../../output.txt");
  50.         string line = "";
  51.         string number = "";
  52.         int sizeOfMatrix;
  53.         int row = 0;
  54.         int col = 0;
  55.        
  56.         using (read)
  57.         {
  58.             // read size of matrix and make one with that size
  59.             line = read.ReadLine();
  60.             sizeOfMatrix = int.Parse(line);
  61.             int[,] matrix = new int[sizeOfMatrix, sizeOfMatrix];
  62.  
  63.             while ((line = read.ReadLine()) != null)
  64.             {
  65.                 // fills matrix with numbers
  66.                 foreach (var symbol in line)
  67.                 {
  68.                     if (symbol != ' ')
  69.                     {
  70.                         number += symbol;
  71.                     }
  72.                     else
  73.                     {
  74.                         matrix[row, col] = int.Parse(number);
  75.                         number = "";
  76.                         col++;
  77.                     }
  78.                 }
  79.                 matrix[row, col] = int.Parse(number);
  80.                 col = 0;
  81.                 row++;
  82.                 number = "";
  83.             }
  84.  
  85.             // print in file the max sum
  86.             using (write)
  87.             {
  88.                 write.WriteLine(FindMaxSum(matrix));
  89.             }
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement