Count the coins/0-1: Difference between revisions

m
→‎{{header|Pascal}}: added checking all weights not taking order into account
m (→‎{{header|Perl}}: prepend pascal)
m (→‎{{header|Pascal}}: added checking all weights not taking order into account)
Line 159:
</pre>
=={{header|Pascal}}==
first count all combinations by brute force.Order is important.Modified nQueens.<BR>Next adding weigths by index.<BR>Using Phix wording 'silly'.
<lang pascal>program Coins0_1;
{$IFDEF FPC}
Line 177:
type
{$IFNDEF FPC}
NativeInt = longIntInt32;
{$ENDIF}
tFreeCol = array[0..nmax] of Int32;
var
n,gblCount : nativeUInt;
FreeIdx,
IdxWeight : tFreeCol;
n,
n,gblCount : nativeUInt;
 
procedure AddNextWeight(Row,sum:nativeInt);
//order is important
var
i,Col,Weight : nativeInt;
Line 195 ⟶ 197:
Col := FreeIdx[i];
Weight:= IdxWeight[col];
IF Sum+Weight ><= 0 then
CONTINUE;
 
Sum +=Weight;
If Sum = 0 then
Begin
Sum -+=Weight;
inc(gblCount);If Sum = 0 then
end Begin
else Sum -=Weight;
begin inc(gblCount);
//swap FreeRow[Row<->i]end
FreeIdx[i] := FreeIdx[Row];else
FreeIdx[Row] := Col;begin
FreeIdx[i] := FreeIdx[Row];
FreeIdx[Row] := Col;
 
AddNextWeight(Row+1,sum);
//Undo
Sum -=Weight;
FreeIdx[Row] := FreeIdx[i];
FreeIdx[i] := Col;
CONTINUEend;
end;
end;
end;
end;
 
procedure CheckBinary(n,MaxIdx,Sum:NativeInt);
//order is not important
Begin
if If Sumsum = 0 then
inc(gblcount);
If (sum < 0) AND (n <= MaxIdx) then
Begin
//test next sum
CheckBinary(n+1,MaxIdx,Sum);// add nothing
CheckBinary(n+1,MaxIdx,Sum+IdxWeight[n]);//or the actual index
end;
end;
Line 225 ⟶ 239:
gblCount := 0;
AddNextWeight(0,-MaxSum);
WriteLnWrite(MaxSum:6,gblCount:12);
gblCount := 0;
CheckBinary(0,i,-MaxSum);
WriteLn(gblCount:12);
end;
 
Line 232 ⟶ 249:
 
begin
writeln('sum':6,'very silly':12,'silly':12);
For i := 0 to High(coins1) do
Begin
Line 252 ⟶ 270:
end;
CheckAll(High(coins3),40);
end.
 
end.</lang>
{{out}}
<pre> 6 10
sum 6very silly 38silly
40 6 3782932 10 3
6 38 9
// real 0m0,079s ( fpc 3.2.0 -O3 -Xs AMD 2200G 3.7 Ghz )
40 3782932 464
 
// real 0m0,079s080s ( fpc 3.2.0 -O3 -Xs AMD 2200G 3.7 Ghz )
</pre>
 
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
Anonymous user