Solve hanging lantern problem: Difference between revisions
Content added Content deleted
m (→fast analytical count only: slightly neater test loop) |
(Added XPL0 example.) |
||
Line 1,208: | Line 1,208: | ||
[7, 6, 1, 4, 3, 2, 5] [7, 6, 1, 4, 3, 5, 2] [7, 6, 1, 4, 5, 3, 2] [7, 6, 1, 5, 4, 3, 2] |
[7, 6, 1, 4, 3, 2, 5] [7, 6, 1, 4, 3, 5, 2] [7, 6, 1, 4, 5, 3, 2] [7, 6, 1, 5, 4, 3, 2] |
||
[7, 6, 5, 4, 1, 3, 2] [7, 6, 5, 4, 3, 1, 2] [7, 6, 5, 4, 3, 2, 1] [7, 6, 5, 1, 4, 3, 2] |
[7, 6, 5, 4, 1, 3, 2] [7, 6, 5, 4, 3, 1, 2] [7, 6, 5, 4, 3, 2, 1] [7, 6, 5, 1, 4, 3, 2] |
||
</pre> |
|||
=={{header|XPL0}}== |
|||
<lang XPL0>char N, Column, Sequences, I, Lanterns; |
|||
proc Tally(Level); |
|||
char Level, Col; |
|||
[for Col:= 0 to N-1 do |
|||
if Column(Col) > 0 then |
|||
[Column(Col):= Column(Col)-1; |
|||
if Level = Lanterns-1 then Sequences:= Sequences+1 |
|||
else Tally(Level+1); |
|||
Column(Col):= Column(Col)+1; |
|||
]; |
|||
]; |
|||
[Sequences:= 0; Lanterns:= 0; |
|||
N:= IntIn(0); |
|||
Column:= Reserve(N); |
|||
for I:= 0 to N-1 do |
|||
[Column(I):= IntIn(0); |
|||
Lanterns:= Lanterns + Column(I); |
|||
]; |
|||
Tally(0); |
|||
IntOut(0, Sequences); |
|||
]</lang> |
|||
{{out}} |
|||
<pre> |
|||
5 |
|||
1 3 5 2 4 |
|||
37837800 |
|||
</pre> |
</pre> |