View difference between Paste ID: 41CQpcE6 and RjwMxZTT
SHOW: | | - or go back to the newest paste.
1
using System;
2
3
class MaxSumSquare
4
{
5
    static void PrintMatrix(int[,] matrix)
6
    {
7
        for (int row = 0; row < matrix.GetLength(0); row++)
8
        {
9
            for (int col = 0; col < matrix.GetLength(1); col++)
10
            {
11
                Console.Write("{0,4}", matrix[row, col]);
12
            }
13
            Console.WriteLine();
14
            Console.WriteLine();
15
        }
16
    }
17
18
    static void Main()
19
    {
20
        //Твоята матрица
21
        int[,] firstTestMatrix = {
22
            {7, 1, 3, 3, 2, 1},
23
            {1, 3, 9, 8, 5, 6},
24
            {4, 6, 7, 9, 1, 0} };
25
26
        //Още една примерна матрица
27
        int[,] secondTestMatrix = {
28
            {7, 1, 3, 3, 2, 1},
29
            {1, 3, 9, 8, 5, 6},
30
            {4, 6, 7, 9, 1, 0},
31
            {9, 9, 9, 9, 9, 9} };
32
33
        //За да може автоматично да обходим двете тестови матрици
34
        int[][,] allTestMatrises = {firstTestMatrix, secondTestMatrix };
35
36
        int[,] maxMatrix = new int[3, 3];       
37
        
38
39
        foreach (var testMatrix in allTestMatrises)
40
        {
41
            int currentSum = 0;
42
            int maxSum = int.MinValue;
43
            int startRow = 0;
44
            int startCol = 0;
45
46
            PrintMatrix(testMatrix);
47
48
            //Трябва да да добавиш още една итерация(използвайки "<=" вместо "<")
49-
            //в циклите обхождащи началната матрица, иначе кодът вътре не се изпълнява.
49+
            //в циклите обхождащи индексите на началната матрица, иначе кодът вътре не се изпълнява.
50-
            //При матрица с един от размерите равен на 3
50+
51
            for (int row = 0; row <= testMatrix.GetLength(0) - 3; row++)
52
            {
53-
                for (int col = 0; col <= secondTestMatrix.GetLength(1) - 3; col++)
53+
                for (int col = 0; col <= testMatrix.GetLength(1) - 3; col++)
54
                {
55
                    //методът за сумиране - непроменен
56
                    currentSum = SumElementsOfSubMatrix(testMatrix, row, col);
57
58
                    if (currentSum > maxSum)
59
                    {
60
                        //Всичко е наред тук
61
                        maxSum = currentSum;
62
                        startRow = row;
63
                        startCol = col;
64
65
                        //Това е проблемната част от кода. Разгледай я.
66
                        CopyMaxSumElements(maxMatrix, testMatrix, startRow, startCol);
67
                    }
68
                    currentSum = int.MinValue;
69
                }
70
            }
71
72
            Console.WriteLine("The elements with maximal sum of the elements are: ");
73
            PrintMatrix(maxMatrix);
74
            Console.WriteLine();
75
            
76
        }
77
        
78
        //Тук също има грешка, не съм я гледал. Изпозлвах PrintMatrix за да ти 
79
        //покажа резултата
80
        //Console.WriteLine("{0,4} {1,4} {2,4}", maxMatrix[startRow, startCol], maxMatrix[startRow, startCol + 1], maxMatrix[startRow, startCol + 2]);
81
        //Console.WriteLine("{0,4} {1,4} {2,4}", maxMatrix[startRow + 1, startCol], maxMatrix[startRow + 1, startCol + 1], maxMatrix[startRow + 1, startCol + 2]);
82
        //Console.WriteLine("{0,4} {1,4} {2,4}", maxMatrix[startRow + 2, startCol], maxMatrix[startRow + 2, startCol + 1], maxMatrix[startRow + 2, startCol + 2]);
83
84
        
85
    }
86
87
    private static void CopyMaxSumElements
88
        (int[,] maxMatrix, int[,] testMatrix, int startRow, int startCol)
89
    {
90
        //"Вътрешните цикли правилно започват от стартовите индекси за
91
        //ред и колона на подматрицата с макс. сума 
92
        for (int i = startRow; i <= startRow + 2; i++)
93
        {
94
            for (int j = startCol; j <= startCol + 2; j++)
95
            {
96
                //Този ред ти хвърля изключение, защото не ползваш 
97
                //правилните индекси. За начална матрица [20,20];
98
                // и startRow =7, startCol=13 ще се опиташ да запишеш на
99
                // maxMatrix[7, 13.]                            
100
                //maxMatrix[i, j] = matrix[row, col];
101
102
                //"Така започваш да записваш от позиция [0,0]
103
                //до позиция [2,2] в maxMatrix"
104
                maxMatrix[i - startRow, j - startCol] = testMatrix[i, j];
105
            }
106
        }
107
    }
108
109
    private static int SumElementsOfSubMatrix
110
        (int[,] testMatrix, int row, int col)
111
    {
112
       int currentSum = testMatrix[row, col] + testMatrix[row, col + 1] +
113
            testMatrix[row, col + 2] + testMatrix[row + 1, col] +
114
            testMatrix[row + 1, col + 1] + testMatrix[row + 1, col + 2] +
115
            testMatrix[row + 2, col] + testMatrix[row + 2, col + 1] +
116
            testMatrix[row + 2, col + 2];
117
        return currentSum;
118
    }
119
120
}