ellapt

T13.5.MaxSumPlatform

Jan 31st, 2013
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. class MaxSumPlatform
  5. {
  6. static double Max2x2Sum(double[,] matrix, int dim)
  7. {
  8. double bestSum = int.MinValue;
  9. int rStart = 0;
  10. int cStart = 0;
  11. for (int row = 0; row < dim - 1; row++)
  12. {
  13. for (int col = 0; col < dim - 1; col++)
  14. {
  15. double sum = matrix[row, col] + matrix[row, col + 1] + matrix[row + 1, col] + matrix[row + 1, col + 1];
  16. if (sum > bestSum)
  17. {
  18. bestSum = sum;
  19. rStart = row;
  20. cStart = col;
  21. }
  22. }
  23. }
  24. return bestSum;
  25. }
  26.  
  27. static void Main()
  28. {
  29. string fileName1 = @"../../Matrix.txt";
  30. string line = "";
  31. string max2Sum="";
  32. int dim = 0;
  33. try
  34. {
  35. StreamReader streamReader = new StreamReader(fileName1);
  36. dim = int.Parse(streamReader.ReadLine());
  37. string[] lineStr = new string[dim];
  38. double[,] matrix = new double[dim, dim];
  39. for (int i = 0; i < dim; i++)
  40. {
  41. line = streamReader.ReadLine();
  42. lineStr=line.Split(' ');
  43. for (int j = 0; j < dim; j++)
  44. {
  45. matrix[i, j] = double.Parse(lineStr[j]);
  46. }
  47. }
  48. streamReader.Close();
  49. max2Sum=Convert.ToString(Max2x2Sum(matrix, dim));
  50.  
  51. StreamWriter streamWriter = new StreamWriter(@"../../maxSum2.txt", false);
  52. streamWriter.WriteLine(max2Sum, true);
  53. streamWriter.Close();
  54. }
  55. catch (Exception ex)
  56. {
  57. Console.Error.WriteLine("Input/Output operation failed", ex.Message);
  58. }
  59. finally
  60. {
  61. Console.WriteLine("The maximal sum of a 2x2 platform is: {0}", max2Sum);
  62. }
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment