Cod sursa(job #51231)

Utilizator adrianraduleaRadulea Adrian adrianradulea Data 10 aprilie 2007 15:02:08
Problema Loto Scor 10
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.19 kb
var f,g:text;
    a:array[1..100] of longint;
    st:array[1..6] of byte;
    n,k,i:byte;
    s,s1:longint;
    as,ev,ok:boolean;
procedure init;
begin
st[k]:=0;
end;
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 do if st[i]=0 then ev:=false;
end;
function solutie(k:integer):boolean;
begin
solutie:=k=6;
end;
procedure tipar;
begin
s1:=0;
for i:=1 to 6 do s1:=s1+a[st[i]];
if s1=s then begin
  for i:=1 to 6 do write(g,a[st[i]],' ');
  ok:=true;
end;
end;
begin
assign(f,'loto.in'); reset(f);
assign(g,'loto.out'); rewrite(g);
read(f,n,s);
for i:=1 to n do read(f,a[i]);
k:=1;
init;
ok:=false;
while k>0 do begin
  repeat
    succesor;
    if as then valid;
  until (not as) or (as and ev);
  if as then if solutie(k) then begin
                             tipar;
                             if ok then break;
                           end
                           else begin
                             k:=k+1;
                             init;
                           end
        else k:=k-1;
end;
if not ok then write(g,'-1');
close(g);
end.