P1039 最小差距

var t,i,j,k,x,y,n,temp,kk,kk1,tt,s,yy:longint;a:array[0..11]of longint;
b:array[0..11]of boolean;
begin
readln(t);
for tt:=1 to t do
begin
readln(n);fillchar(a,sizeof(a),0);
for i:=1 to n do read(a[i]);
readln;
for i:=1 to n-1 do
for j:=i+1 to n do
if a[i]<a[j] then begin temp:=a[i];a[i]:=a[j];a[j]:=temp;end;
if n mod 2=1 then
begin
x:=a[1];
if a[n]=0 then y:=a[n-1] else y:=a[n];
yy:=y;
for j:=2 to n div 2 do x:=x*10+a[j];
for j:=n downto n div 2+1 do
if yy<>a[j] then y:=y*10+a[j];
writeln(abs(x-y));
end;
if n mod 2=0 then
begin
s:=1000000;
if(n=2)and(a[2]=0)then begin writeln(a[1]);continue;end;
for j:=1 to n-1 do
if a[j+1]=0 then break
else
begin
fillchar(b,sizeof(b),true);
x:=a[j];y:=a[j+1];b[j]:=false;b[j+1]:=false;
kk:=1;kk1:=1;
for k:=n downto 1 do
if b[k]then
begin
x:=x*10+a[k];b[k]:=false;inc(kk);
if kk=n div 2 then break;
end;
for k:=1 to n do
if b[k] then
begin
y:=y*10+a[k];b[k]:=false;inc(kk1);
if kk1=n div 2 then break;
end;
if s>abs(x-y) then s:=abs(x-y);
end;
writeln(s);
end;
end;
end.

0 条评论

目前还没有评论...

信息

ID
1039
难度
7
分类
贪心 点击显示
标签
(无)
递交数
3708
已通过
832
通过率
22%
被复制
11
上传者