-
Notifications
You must be signed in to change notification settings - Fork 0
/
02.MaximalSum
54 lines (48 loc) · 2.02 KB
/
02.MaximalSum
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
using System.Linq;
class maximalSum
{
static void Main()
{
string [] size = Console.ReadLine().Split();
int rows = int.Parse(size[0]);
int cols = int.Parse(size[1]);
int[,] matrix = new int[rows, cols];
for (int row = 0; row < rows; row++)
{
string[] numbers = Console.ReadLine().Split();
for (int col = 0; col < cols; col++)
{
matrix[row, col] = int.Parse(numbers[col]);
}
}
int bestSum = int.MinValue;
int sum = 0;
int bestRow = 0;
int bestCol = 0;
for (int row = 0; row < rows - 2; row++)
{
for (int col = 0; col < cols - 2; col++)
{
sum = matrix[row, col] + matrix[row, col + 1] + matrix[row, col + 2] +
matrix[row + 1, col] + matrix[row + 1, col + 1] + matrix[row + 1, col + 2] +
matrix[row + 2, col] + matrix[row + 2, col + 1] + matrix[row + 2, col + 2];
if (sum > bestSum)
{
bestSum = sum;
bestRow = row;
bestCol = col;
}
}
}
Console.WriteLine("The best platform is:");
Console.WriteLine(" {0} {1} {2}",
matrix[bestRow, bestCol], matrix[bestRow, bestCol + 1], matrix[bestRow, bestCol + 2]);
Console.WriteLine(" {0} {1} {2}",
matrix[bestRow + 1, bestCol], matrix[bestRow + 1, bestCol + 1], matrix[bestRow +1, bestCol + 2]);
Console.WriteLine(" {0} {1} {2}",
matrix[bestRow + 2, bestCol], matrix[bestRow + 2, bestCol + 1], matrix[bestRow + 2, bestCol + 2]);
Console.WriteLine("The maximal sum is: {0}", bestSum);
}
}