- 装箱问题
- 2016-11-12 15:00:58 @
var
i,j,n,v:longint;
a:array[0..31]of longint;
opt:array[0..31,0..20001]of longint;
function
max(x,y:longint):longint;
begin
if x>y then exit(x)
else exit(y);
end;
begin
readln(v);
readln(n);
for i:=1 to n do
readln(a[i]);
for j:=1 to v do
if j>=a[1]
then
opt[1,j]:=a[1];
for i:=2 to n do
for j:=1 to v do
if j>a[i]
then
opt[i,j]:=max(opt[i-1,j],opt[i-1,j-a[i]]+a[i])
else
opt[i,j]:=opt[i-1,j];
writeln(v-opt[n,v]);
end.
2 条评论
-
hahayang LV 10 @ 2016-11-13 15:35:54
二维数组别闹!
var
w:array[1..30] of longint;
f:array[0..20000] of longint;
n, m, i, j:longint;
begin
readln(m);
readln(n);
for i:=1 to n do readln(w[i]);
fillchar(f, sizeof(f), 0);
for i:=1 to n do
for j:=m downto w[i] do if f[j-w[i]]+w[i]>f[j] then f[j]:=f[j-w[i]]+w[i];
write(m-f[m])
end. -
2016-11-12 15:42:35@
标题打错了
pascal
- 1