Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class MaxSumSquare
- {
- static void PrintMatrix(int[,] matrix)
- {
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- Console.Write("{0,4}", matrix[row, col]);
- }
- Console.WriteLine();
- Console.WriteLine();
- }
- }
- static void Main()
- {
- //Твоята матрица
- int[,] firstTestMatrix = {
- {7, 1, 3, 3, 2, 1},
- {1, 3, 9, 8, 5, 6},
- {4, 6, 7, 9, 1, 0} };
- //Още една примерна матрица
- int[,] secondTestMatrix = {
- {7, 1, 3, 3, 2, 1},
- {1, 3, 9, 8, 5, 6},
- {4, 6, 7, 9, 1, 0},
- {9, 9, 9, 9, 9, 9} };
- //За да може автоматично да обходим двете тестови матрици
- int[][,] allTestMatrises = {firstTestMatrix, secondTestMatrix };
- int[,] maxMatrix = new int[3, 3];
- foreach (var testMatrix in allTestMatrises)
- {
- int currentSum = 0;
- int maxSum = int.MinValue;
- int startRow = 0;
- int startCol = 0;
- PrintMatrix(testMatrix);
- //Трябва да да добавиш още една итерация(използвайки "<=" вместо "<")
- //в циклите обхождащи началната матрица, иначе кодът вътре не се изпълнява.
- //При матрица с един от размерите равен на 3
- for (int row = 0; row <= testMatrix.GetLength(0) - 3; row++)
- {
- for (int col = 0; col <= secondTestMatrix.GetLength(1) - 3; col++)
- {
- //методът за сумиране - непроменен
- currentSum = SumElementsOfSubMatrix(testMatrix, row, col);
- if (currentSum > maxSum)
- {
- //Всичко е наред тук
- maxSum = currentSum;
- startRow = row;
- startCol = col;
- //Това е проблемната част от кода. Разгледай я.
- CopyMaxSumElements(maxMatrix, testMatrix, startRow, startCol);
- }
- currentSum = int.MinValue;
- }
- }
- Console.WriteLine("The elements with maximal sum of the elements are: ");
- PrintMatrix(maxMatrix);
- Console.WriteLine();
- }
- //Тук също има грешка, не съм я гледал. Изпозлвах PrintMatrix за да ти
- //покажа резултата
- //Console.WriteLine("{0,4} {1,4} {2,4}", maxMatrix[startRow, startCol], maxMatrix[startRow, startCol + 1], maxMatrix[startRow, startCol + 2]);
- //Console.WriteLine("{0,4} {1,4} {2,4}", maxMatrix[startRow + 1, startCol], maxMatrix[startRow + 1, startCol + 1], maxMatrix[startRow + 1, startCol + 2]);
- //Console.WriteLine("{0,4} {1,4} {2,4}", maxMatrix[startRow + 2, startCol], maxMatrix[startRow + 2, startCol + 1], maxMatrix[startRow + 2, startCol + 2]);
- }
- private static void CopyMaxSumElements
- (int[,] maxMatrix, int[,] testMatrix, int startRow, int startCol)
- {
- //"Вътрешните цикли правилно започват от стартовите индекси за
- //ред и колона на подматрицата с макс. сума
- for (int i = startRow; i <= startRow + 2; i++)
- {
- for (int j = startCol; j <= startCol + 2; j++)
- {
- //Този ред ти хвърля изключение, защото не ползваш
- //правилните индекси. За начална матрица [20,20];
- // и startRow =7, startCol=13 ще се опиташ да запишеш на
- // maxMatrix[7, 13.]
- //maxMatrix[i, j] = matrix[row, col];
- //"Така започваш да записваш от позиция [0,0]
- //до позиция [2,2] в maxMatrix"
- maxMatrix[i - startRow, j - startCol] = testMatrix[i, j];
- }
- }
- }
- private static int SumElementsOfSubMatrix
- (int[,] testMatrix, int row, int col)
- {
- int currentSum = testMatrix[row, col] + testMatrix[row, col + 1] +
- testMatrix[row, col + 2] + testMatrix[row + 1, col] +
- testMatrix[row + 1, col + 1] + testMatrix[row + 1, col + 2] +
- testMatrix[row + 2, col] + testMatrix[row + 2, col + 1] +
- testMatrix[row + 2, col + 2];
- return currentSum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement