- 搭建双塔
- 2012-07-21 09:08:46 @
二维代码:
var
n,i,j,k,h,tot:integer;
f:array[0..2000,0..2000] of boolean;
a:array[1..100] of integer;
begin
readln(n);
fillchar(f,sizeof(f),false);
for i:=1 to n do
begin
read(a[i]);
h:=h+a[i];
tot:=h div 2;
end;
f[0,0]:=true;
for k:=1 to n do
begin
for i:=tot downto 0 do
for j:=tot downto 0 do
if not f then
if (i>=a[k]) and (f[i-a[k],j])
then f:=true
else if (j>=a[k]) and (f[i,j-a[k]])
then f:=true;
end;
for i:=h downto 1 do
if f then
begin
writeln(i);
halt;
end;
writeln('Impossible');
end.
一维代码:
var
f:array[0..2000]of boolean;
a:array[1..100]of integer;
n,i,j,h:integer;
begin
readln(n);
fillchar(f,sizeof(f),false);
for i:=1 to n do
read(a[i]);
h:=0;
f[0]:=true;
for i:=1 to n do
begin
h:=h+a[i];
for j:=h downto a[i] do
if f[j-a[i]] then f[j]:=true;
end;
i:=1001;
repeat
dec(i);
until (i=0) or ((f[i]) and (f));
if (i=0)
then writeln('Impossible')
else writeln(i);
end.
2 条评论
-
diggerdu LV 8 @ 2013-03-30 20:21:13
3,5,7 是错的
-
2012-07-23 08:56:29@
给个反例吧
如果的
1 1 5 8
你的应该是输出7 吧
这显然是错的
- 1