Combinations with repetitions: Difference between revisions

Added solution for Action!
m (→‎Dynamic Programming: Tidied manual recursion version)
(Added solution for Action!)
Line 156:
cwr( 3, 2)= 6
cwr(10, 3)= 220
</pre>
 
=={{header|Action!}}==
<lang Action!>PROC PrintComb(BYTE ARRAY c BYTE len)
BYTE i,ind
 
FOR i=0 TO len-1
DO
IF i>0 THEN Put('+) FI
ind=c(i)
IF ind=0 THEN
Print("iced")
ELSEIF ind=1 THEN
Print("jam")
ELSE
Print("plain")
FI
OD
PutE()
RETURN
 
BYTE FUNC NotDecreasing(BYTE ARRAY c BYTE len)
BYTE i
 
IF len<2 THEN RETURN (1) FI
 
FOR i=0 TO len-2
DO
IF c(i)>c(i+1) THEN
RETURN (0)
FI
OD
RETURN (1)
 
BYTE FUNC NextComb(BYTE ARRAY c BYTE n,k)
INT pos,i
 
DO
pos=k-1
DO
c(pos)==+1
IF c(pos)<n THEN
EXIT
ELSE
pos==-1
IF pos<0 THEN RETURN (0) FI
FI
FOR i=pos+1 TO k-1
DO
c(i)=c(pos)
OD
OD
UNTIL NotDecreasing(c,k)
OD
RETURN (1)
 
PROC Comb(BYTE n,k,show)
BYTE ARRAY c(10)
BYTE i,count
 
IF k>n THEN
Print("Error! k is greater than n.")
Break()
FI
 
PrintF("Choices of %B from %B:%E",k,n)
FOR i=0 TO k-1
DO
c(i)=0
OD
 
count=0
DO
count==+1
IF show THEN
PrintF(" %B. ",count)
PrintComb(c,k)
FI
UNTIL NextComb(c,n,k)=0
OD
PrintF("Total choices %B%E%E",count)
RETURN
 
PROC Main()
Comb(3,2,1)
Comb(10,3,0)
RETURN</lang>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Combinations_with_repetitions.png Screenshot from Atari 8-bit computer]
<pre>
Choices of 2 from 3:
1. iced+iced
2. iced+jam
3. iced+plain
4. jam+jam
5. jam+plain
6. plain+plain
Total choices 6
 
Choices of 3 from 10:
Total choices 220
</pre>
 
Anonymous user