Cod sursa(job #289126)

Utilizator hominiduBirsan Dragos hominidu Data 26 martie 2009 14:44:18
Problema Combinari Scor 10
Compilator fpc Status done
Runda aa Marime 0.95 kb
var f,g :text;
    st:array[1..100] of byte;
    p,K,i,n:integer;
    ev{alid},as{uccesor}:boolean;
procedure succesor;
 begin
    if st[k]<n then
     begin
      as:=true;
      st[k]:=st[k]+1;
     end
     else
     as:=false;
 end;
procedure valid;
 begin
  ev:=true;
  for i:=1 to k-1 do
   if (st[i]>=st[i+1])  then
   ev:=false;
 end;
procedure init;
begin
st[k]:=0;
end;

function solutie:boolean;
begin
solutie:=(p=k);
end;

procedure tipar;
begin
 assign(g,'combinari.out');
 rewrite(g);
 for i:=1 to p do
 write(g,st[i],' ');
 writeln(g);
 close(g);
end;

BEGIN
assign(f,'combinari.in');
reset(f);
read(f,n,p);
close(f);

k:=1;init;
while k>0 do
 begin
   repeat
    succesor;
    if as then valid
   until (as and ev) or (not as);
  if (as) and (ev) then
     if solutie  then
       tipar
       else
        begin
         k:=k+1;
         init
        end
    else
    k:=k-1;
 end;
END.