PASCAL 01背包

2024-12-11 15:53:41
推荐回答(2个)
回答1:

var
f:array [1..35000] of longint;
b,c:longint;
v:array [1..21] of longint;
i,j:longint;
begin
read(c,b);
for i:=1 to b do read(v[i]);
for i:=1 to b do for j:=c downto v[i] do
if f[j] writeln(f[c]);
end.

回答2:

var
a:array[0..35000]of boolean;
f:array[1..21]of longint;
b,c,i,j:longint;
begin
fillchar(a,sizeof(a),false);
a[0]:=true;
read(c,b);
for i:=1 to b do
read(f[i]);
for i:=1 to b do
for j:=c downto f[i] do
a[j]:=a[j-f[i]] or a[j];
for i:=c downto 0 do
if a[i] then begin
writeln(i); halt;
end;
end.