usaco 2.3 zerosum 2008.6.21

来源:互联网 发布:网络加速器官网 编辑:程序博客网 时间:2024/06/05 21:50

usaco 2.3 zerosum 2008.6.21

{

ID:

PROG: zerosum

LANG: PASCAL

}

program p_zerosum;

const r:array[1..9]ofchar=('1','2','3','4','5','6','7','8','9');

var

 ans:array[1..9000]of string;

 f1,f2:text;

 x,y:string;

 i,n,p,l:longint;

 

procedure qsort(i1,j1:longint);

var

 l1,r1:longint;x1,y1:string;

   begin

      l1:=i1;r1:=j1;

      x1:=ans[(l1+r1) div 2];

      repeat

      while ans[l1]<x1 do inc(l1);

      while ans[r1]>x1 do dec(r1);

       if l1<=r1 then

                 begin

                       y1:=ans[l1];ans[l1]:=ans[r1];ans[r1]:=y1;inc(l1);dec(r1);

                 end;

      until l1>r1;

      if r1>i1 then qsort(i1,r1);

      if l1<j1 then qsort(l1,j1);

      end;

procedure init;

begin

  assign(f1,'zerosum.in');reset(f1);

  assign(f2,'zerosum.out');rewrite(f2);

  read(f1,n);p:=n-1;l:=0;x:='+- ';

end;

procedure put;

var i:longint;

begin

 inc(l);ans[l]:='';

  fori:=1 to p do

   ans[l]:=ans[l]+y[i]+r[i+1];

    ans[l]:='1'+ans[l];

end;

procedure count;

  vart,j:longint;

 begin

 

   if y[1]<>' ' then begin t:=1;i:=1 end

     else begin

            t:=12;i:=2;

          end;

    repeat inc(i);j:=i-1;

      if (y[j]='+')and(y[i]<>' ') then inc(t,i);

      if (y[j]='+')and(y[i]=' ') then begin t:=t+i*10+i+1;inc(i);end;

      if (y[j]='-')and(y[i]<>' ') then dec(t,i);

      if (y[j]='-')and(y[i]=' ') then begin t:=t-i*10-i-1;inc(i);end;

    until i>=n;

    if t=0 then put;

  end;

procedure deal(k:longint);

var i:longint;

  begin

    if k>p then begin count;exit;end;

    for i:=1 to 3 do

        if (i<>3)or(y[k-1]<>' ') then

         begin y[k]:=x[i];

                  deal(k+1);end;

  end;

{------------------------main---------------------------}

begin

 init;

 deal(1);

 qsort(1,l);

  fori:=1 to l do

    writeln(f2,ans[i]);

  close(f1);close(f2);

end.

0 0
原创粉丝点击