Whenever Mother goes shopping she brings home half a dozen boxes of fancy cakes, invariably dividing her custom between B's Tea Rooms, S's Bakery
and P's Stores whose boxes contain 3, 4, and
5 cakes respectively.
During last month she made seven such expeditions and came home with a different number of cakes each time, 80 in all coming from P's Stores.
How many were provided by S's Bakery?
The shopping trips can be represented by the following seven sets of digits
114 123 213 222 312 321 411
Each set of three digits represents the number of boxes bought at B, at S and at P respectively. We of course don't know which of the 7! = 5040 possible orders these trips were made in, but we see that Mother bought 4+3+3+2+2+1+1 = 16 boxes at P's Stores to get those 80 cakes.
She also bought 1+2+1+2+1+2+1 = 10 boxes at S's Bakery, giving the answer of 10*4 = 40 cakes.
Not that it was asked, but she bought 1+1+2+2+3+3+4 = 16 boxes at B's Tea Room, for 48 cakes. That's 168 cakes in all for the month.
We can verify that indeed each day's number of cakes was different:
27, 26, 25, 24, 23, 22 and 21
The answer assumes that Mother bought at least one box from each of the three stores. Allowing her to skip either B or S on a given day would allow many more solutions. The program itself did not guard against skipping P on a given day, but it found no such solutions.
DefDbl A-Z
Dim crlf$, numBoxes(7, 3), totCakes(7), overtotP(7), overtotS(7)
Private Sub Form_Load()
Form1.Visible = True
Text1.Text = ""
crlf = Chr$(13) + Chr$(10)
setup 1
Text1.Text = Text1.Text & crlf & " done"
End Sub
Sub setup(trip)
For b = 1 To 6
For s = 1 To 6 - b
p = 6 - b - s
numBoxes(trip, 1) = b
numBoxes(trip, 2) = s
numBoxes(trip, 3) = p
totCakes(trip) = 3 * b + 4 * s + 5 * p
overtotP(trip) = overtotP(trip - 1) + 5 * p
overtotS(trip) = overtotS(trip - 1) + 4 * s
DoEvents
If overtotP(trip) <= 80 Then
good = 1
For i = 1 To trip - 1
If totCakes(i) = totCakes(trip) Then good = 0: Exit For
Next i
If good Then
If trip = 7 Then
If overtotP(trip) = 80 Then
ReDim nm(7): good = 1
For tr = 1 To 7
For i = 1 To 3
nm(tr) = 10 * nm(tr) + numBoxes(tr, i)
Next
If nm(tr) < nm(tr - 1) Then good = 0: Exit For
Next tr
If good Then
For i = 1 To 7
Text1.Text = Text1.Text & nm(i) & " "
Next i
Text1.Text = Text1.Text & overtotS(trip) & " " & crlf
End If
End If
Else
setup trip + 1
End If
End If
End If
ct = ct + 1
Next s
Next b
End Sub
The program could have been more efficient, as the requirement that the representations be in order when treated as 3-digit numbers was added as an afterthought. Ideally it would have been checked as it went along, to prune the recursive tree and saving much time.
|
Posted by Charlie
on 2017-09-01 13:53:37 |