How to resolve the algorithm Combinations with repetitions step by step in the Action! programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Combinations with repetitions step by step in the Action! programming language
Table of Contents
Problem Statement
The set of combinations with repetitions is computed from a set,
S
{\displaystyle S}
(of cardinality
n
{\displaystyle n}
), and a size of resulting selection,
k
{\displaystyle k}
, by reporting the sets of cardinality
k
{\displaystyle k}
where each member of those sets is chosen from
S
{\displaystyle S}
. In the real world, it is about choosing sets where there is a “large” supply of each type of element and where the order of choice does not matter. For example: Note that both the order of items within a pair, and the order of the pairs given in the answer is not significant; the pairs represent multisets. Also note that doughnut can also be spelled donut.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Combinations with repetitions step by step in the Action! programming language
Source code in the action! programming language
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)
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
You may also check:How to resolve the algorithm Hello world/Web server step by step in the BASIC programming language
You may also check:How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Yorick programming language
You may also check:How to resolve the algorithm Rosetta Code/Rank languages by popularity step by step in the Erlang programming language
You may also check:How to resolve the algorithm Maximum triangle path sum step by step in the Action! programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the PL/SQL programming language