H
hung1xpro96
Đây -- bài giải bài 4 : Cânpost lời giả bài cân cho các bạn hocmai cùng tham khảo đi Tmod lập trình ) )
Mã:
Program bi1;
Const fi='can.inp';fo='can.out';
Var a:array[1..100] Of Integer;
l:array[0..100,0..100] Of Integer;
chon:array[1..100] Of Boolean;
t1,t2,i,j,n,s,s2:Integer;g,f:Text;
Procedure doctep;
Begin
Assign(f,fi);
Reset(f);s:=0;
Readln(f,n);
For i:=1 to n do
Read(f,a[i]);
For i:=1 to n do
s:=s+a[i]; s2:=s div 2;
Close(f);
End;
Procedure khoitao;
Begin
For j:=0 to s2 do
l[0,j]:=0;
Fillchar(l,sizeof(l),0);
For j:=0 to s2 do
If j < a[i] then l[1,j]:=0
Else
l[1,j]:=a[1];
End;
Procedure xd;
Begin
For i:=2 to n do
For j:=0 to s2 do
begin
l[i,j]:=l[i-1,j];
If j-a[i]>=0 then
If l[i,j]<= (a[i]+l[i-1,j-a[i]]) then
l[i,j]:=a[i]+l[i-1,j-a[i]];
End;
End;
Procedure truy;
Begin
t1:=l[n,s2];
t2:=s-t1;
If abs(t1-t2) <> 0 then Writeln(g,0)
Else
Begin
i:=n; j:=s2;
While i>0 do
Begin
If l[i,j]=l[i-1,j] then dec(i)
Else
Begin
chon[i]:=true;
j:=j-a[i];
dec(i);
End;
end;
For i:=1 to n do
If chon[i]=false then
Write(g,a[i]:3);
Writeln(g);
For i:=1 to n do
If chon[i]=true then
Write(g,a[i]:3);
End;
Close(G);
End;
Begin
doctep;
khoitao;
xd;
Fillchar(chon,sizeof(chon),false);
Assign(g,fo); Rewrite(g);
truy;
End.