'Rectangle width and height
Const W As Single = 1200
Const H As Single = 800
'Brick width (a) and height (b)
Const a As Single = 37
Const b As Single = 83
Private Sub main()
Dim nRowsPort As Integer, nRowsLand As Integer
Dim nBricksPerRowPort As Integer, nBricksPerRowLand As Integer
Dim nRowsPortMax As Integer, nRowsLandMax As Integer
'Height of all the rows (portrait plus landscape)
Dim TotalH As Integer
'Total number of bricks
Dim TotalN As Integer
'Total bricks with possible extras
Dim GrandTotalN As Integer
Dim tmp As Integer, i As Integer, j As Integer
Dim dummy1 As Single, dummy2 As Single
nBricksPerRowPort = Int(W / a)
nBricksPerRowLand = Int(W / b)
'Maximum values for nRowsPort and nRowsLand:
nRowsPortMax = Int(H / b)
nRowsLandMax = Int(H / a)
'Try all integer combinations of nRowsPort and nRowsLand
'with the restrictions:
'nRowsPort <= nRowsPortMax
'nRowsLand <= nRowsLandMax
'b*nRowsPort + a*nRowsLand <= H
TotalN = 0
For i = 1 To nRowsPortMax
For j = 1 To nRowsLandMax
TotalH = b * i + a * j
If TotalH <= H Then
tmp = i * nBricksPerRowPort + j * nBricksPerRowLand
If tmp > TotalN Then
TotalN = tmp
nRowsPort = i
nRowsLand = j
End If
End If
Next
Next
'Check if some space is left to fit in a few more
dummy1 = W - b * nBricksPerRowLand
dummy2 = H - b * nRowsPort
If dummy1 >= a And dummy2 >= b Then
'Number of extra rows of bricks
i = Int(dummy2 / b)
'Number of extra bricks per row
j = Int(dummy1 / a)
'Number of extra bricks
i = i * j
Else
i = 0
End If
'Grand total
GrandTotalN = TotalN + i
Debug.Print "Total bricks: " & CStr(TotalN)
Debug.Print "Extra bricks: " & CStr(i)
Debug.Print "Grand total bricks: " & CStr(GrandTotalN)
Debug.Print "Rows (portrait): " & CStr(nRowsPort)
Debug.Print "Rows (landscape): " & CStr(nRowsLand)
End Sub