比赛 NOIP_4 评测结果 AAAWWWAWWW
题目名称 算24点 最终得分 40
用户昵称 Achilles 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2008-09-19 21:47:07
显示代码纯文本
program point24;
var
  a:array[0..4]of integer;
  b:array[1..3]of integer;
  i:array[1..4]of integer;
  j:array[1..3]of integer;
  sz:array[1..4,1..4] of integer;
  f:array[1..4]of char;
  k:integer;
begin
  assign(input,'point24.in');
  assign(output,'point24.out');
  reset(input);
  rewrite(output);
  readln(a[1],a[2],a[3],a[4]);
  f[1]:='+';
  f[2]:='-';
  f[3]:='*';
  f[4]:='/';
  for i[1]:=1 to 4 do
    for i[2]:=1 to 4 do
      for i[3]:=1 to 4 do
        for i[4]:=1 to 4 do
          if (i[1]<>i[2])and(i[1]<>i[3])and(i[1]<>i[4])and(i[2]<>i[3])and(i[2]<>i[4])and(i[3]<>i[4])then begin
            for j[1]:=1 to 4 do
              for j[2]:=1 to 4 do
                for j[3]:=1 to 4 do
                begin
                  a[0]:=a[1];
                  for k:=1 to 3 do
                  begin
                    if j[k]=1 then a[0]:=a[0]+a[i[k+1]];
                    if j[k]=2 then a[0]:=a[0]-a[i[k+1]];
                    if j[k]=3 then a[0]:=a[0]*a[i[k+1]];
                    if (j[i[k]]=4)and(a[i[k+1]]<>0) then if a[0] mod a[k+1]=0 then a[0]:=a[0] div a[k+1];
                  end;
                  if a[0]=24 then begin
                    write(a[i[1]]);
                    write(f[j[1]]);
                    write(a[i[2]]);
                    write('=');
                    if j[1]=1 then begin
                      write(a[i[1]]+a[i[2]]);
                      b[1]:=a[i[1]]+a[i[2]];
                    end;
                    if j[1]=2 then begin
                      write(a[i[1]]-a[i[2]]);
                      b[1]:=a[i[1]]-a[i[2]];
                    end;
                    if j[1]=3 then begin
                      write(a[i[1]]*a[i[2]]);
                      b[1]:=a[i[1]]*a[i[2]];
                    end;
                    if j[1]=4 then begin
                      write(a[i[1]] div a[i[2]]);
                      b[1]:=a[i[1]] div a[i[2]];
                    end;
                    writeln;
                    write(b[1]);
                    write(f[j[2]]);
                    write(a[i[3]]);
                    write('=');
                    if j[2]=1 then begin
                      write(b[1]+a[i[3]]);
                      b[2]:=b[1]+a[i[3]];
                    end;
                    if j[2]=2 then begin
                      write(b[1]-a[i[3]]);
                      b[2]:=b[1]-a[i[3]];
                    end;
                    if j[2]=3 then begin
                      write(b[1]*a[i[3]]);
                      b[2]:=b[1]*a[i[3]];
                    end;
                    if j[2]=4 then begin
                      write(b[1] div a[i[3]]);
                      b[2]:=b[1] div a[i[3]];
                    end;
                    writeln;
                    write(b[2]);
                    write(f[j[3]]);
                    write(a[i[4]]);
                    write('=');
                    if j[3]=1 then begin
                      write(b[2]+a[i[4]]);
                      b[3]:=b[2]+a[i[4]];
                    end;
                    if j[3]=2 then begin
                      write(b[2]-a[i[4]]);
                      b[3]:=b[2]-a[i[4]];
                    end;
                    if j[3]=3 then begin
                      write(b[2]*a[i[4]]);
                      b[3]:=b[2]*a[i[4]];
                    end;
                    if j[3]=4 then begin
                      write(b[2] div a[i[4]]);
                      b[3]:=b[2] div a[i[4]];
                    end;
                  close(input);
                  close(output);
                  halt;
                end;
              end;
          end;
  writeln('No answer!');
  close(input);
  close(output);
end.