Imports System
Public Class Program
Shared rows As Integer = 20
Shared columns As Integer = 20
Public Shared Sub PrintGrid(ByVal grid As Integer(,))
For i As Integer = 0 To grid.GetLength(0) - 1
For j As Integer = 0 To grid.GetLength(1) - 1
Console.Write(grid(i, j).ToString().PadLeft(4) & " ")
Next
Console.WriteLine()
Next
End Sub
Private Shared Function generateRandomInteger(ByVal size As Integer) As Integer(,)
Dim random As Random = New Random()
Dim matrix As Integer(,) = New Integer(rows - 1, columns - 1) {}
For i As Integer = 0 To rows - 1
For j As Integer = 0 To columns - 1
matrix(i, j) = random.Next(1, 101)
Next
Next
Return matrix
End Function
Private Shared Function FindMaxProduct(ByVal grid As Integer(,), ByVal size As Integer) As Integer
Dim product As Integer, max As Integer = 0
Dim n1 As Integer = 0, n2 As Integer = 0, n3 As Integer = 0, n4 As Integer = 0
For i As Integer = 0 To size - 1
For j As Integer = 0 To size - 3 - 1
product = grid(i, j) * grid(i, j + 1) * grid(i, j + 2) * grid(i, j + 3)
If product > max Then
n1 = grid(i, j)
n2 = grid(i, j + 1)
n3 = grid(i, j + 2)
n4 = grid(i, j + 3)
max = product
End If
Next
Next
For i As Integer = 0 To size - 1
For j As Integer = 0 To size - 3 - 1
product = grid(j, i) * grid(j + 1, i) * grid(j + 2, i) * grid(j + 3, i)
If product > max Then
n1 = grid(i, j)
n2 = grid(j + 1, i)
n3 = grid(j + 2, i)
n4 = grid(j + 2, i)
max = product
End If
Next
Next
For i As Integer = 0 To size - 3 - 1
For j As Integer = 0 To size - 3 - 1
product = grid(j, i) * grid(j + 1, i + 1) * grid(j + 2, i + 2) * grid(j + 3, i + 3)
If product > max Then
n1 = grid(j, i)
n2 = grid(j + 1, i + 1)
n3 = grid(j + 2, i + 2)
n4 = grid(j + 3, i + 3)
max = product
End If
Next
Next
For i As Integer = 0 To size - 3 - 1
For j As Integer = 3 To size - 1
product = grid(j, i) * grid(j - 1, i + 1) * grid(j - 2, i + 2) * grid(j - 3, i + 3)
If product > max Then
n1 = grid(j, i)
n2 = grid(j - 1, i + 1)
n3 = grid(j - 2, i + 2)
n4 = grid(j - 3, i + 3)
max = product
End If
Next
Next
Console.Write(Environment.NewLine & n1 & " * " & n2 & " * " & n3 & " * " & n4 & " = ")
Return max
End Function
Public Shared Sub Main(ByVal args As String())
Dim grid As Integer(,) = generateRandomInteger(20)
PrintGrid(grid)
Console.Write(FindMaxProduct(grid, 20))
End Sub
End Class
' run:
'
' 55 98 83 68 15 3 66 60 81 6 75 30 82 23 8 69 79 89 64 34
' 12 11 28 44 78 55 79 29 60 43 23 51 97 3 32 1 89 67 29 14
' 61 69 70 3 68 64 71 77 66 26 88 46 53 6 45 45 70 39 90 60
' 25 37 100 39 83 25 33 79 91 8 80 13 60 51 74 44 41 25 76 15
' 85 3 64 35 56 78 98 91 59 87 31 51 77 70 89 24 70 32 21 44
' 31 92 86 58 47 76 87 3 33 2 5 46 64 76 76 22 73 66 84 6
' 27 42 21 5 77 30 36 90 62 50 74 10 5 33 84 94 17 6 88 80
' 91 96 59 57 83 86 88 2 95 68 51 4 49 15 17 89 71 82 82 18
' 40 97 42 83 28 95 41 31 92 82 5 67 79 4 15 32 84 65 22 92
' 42 35 96 95 99 71 62 90 17 96 23 36 25 71 40 95 55 76 29 88
' 45 57 10 13 63 84 26 45 100 85 6 7 60 91 76 6 2 48 85 58
' 34 52 15 97 60 70 43 9 65 21 77 8 36 34 48 86 26 83 33 15
' 46 17 91 32 90 17 76 35 95 35 93 8 92 72 55 94 43 13 54 93
' 42 17 81 79 9 98 71 27 44 91 80 19 15 71 12 18 61 84 7 43
' 94 9 14 30 29 70 17 64 80 54 44 13 30 40 74 29 37 12 24 93
' 46 50 92 5 55 75 93 21 60 37 33 59 6 11 99 33 4 51 51 39
' 81 8 47 90 47 68 90 76 97 84 82 50 60 15 98 45 18 10 76 54
' 77 96 4 43 22 85 8 19 41 41 26 86 61 85 13 39 3 3 58 88
' 86 17 25 77 56 83 99 92 14 55 16 94 76 97 62 85 4 4 68 63
' 60 71 35 57 59 96 90 76 3 59 42 16 52 89 69 60 72 28 87 40
'
' 99 * 95 * 88 * 90 = 74487600
'