pascal石子归并 石子归并一:给出n堆石子的重量W1,W2.WN,要求你合并其中的任意两堆或者n堆(n>=2),求出所有经过合并能够得到的重量值.程序Procedure DP_stone1(n:longint);var I,j,total:longint;beginf[0]:=tru
来源:学生作业帮助网 编辑:作业帮 时间:2024/07/05 02:38:59
![pascal石子归并 石子归并一:给出n堆石子的重量W1,W2.WN,要求你合并其中的任意两堆或者n堆(n>=2),求出所有经过合并能够得到的重量值.程序Procedure DP_stone1(n:longint);var I,j,total:longint;beginf[0]:=tru](/uploads/image/z/15259432-40-2.jpg?t=pascal%E7%9F%B3%E5%AD%90%E5%BD%92%E5%B9%B6+%E7%9F%B3%E5%AD%90%E5%BD%92%E5%B9%B6%E4%B8%80%EF%BC%9A%E7%BB%99%E5%87%BAn%E5%A0%86%E7%9F%B3%E5%AD%90%E7%9A%84%E9%87%8D%E9%87%8FW1%2CW2.WN%2C%E8%A6%81%E6%B1%82%E4%BD%A0%E5%90%88%E5%B9%B6%E5%85%B6%E4%B8%AD%E7%9A%84%E4%BB%BB%E6%84%8F%E4%B8%A4%E5%A0%86%E6%88%96%E8%80%85n%E5%A0%86%EF%BC%88n%3E%3D2%EF%BC%89%2C%E6%B1%82%E5%87%BA%E6%89%80%E6%9C%89%E7%BB%8F%E8%BF%87%E5%90%88%E5%B9%B6%E8%83%BD%E5%A4%9F%E5%BE%97%E5%88%B0%E7%9A%84%E9%87%8D%E9%87%8F%E5%80%BC.%E7%A8%8B%E5%BA%8FProcedure+DP_stone1%28n%3Alongint%29%3Bvar+I%2Cj%2Ctotal%3Alongint%3Bbeginf%5B0%5D%3A%3Dtru)
pascal石子归并 石子归并一:给出n堆石子的重量W1,W2.WN,要求你合并其中的任意两堆或者n堆(n>=2),求出所有经过合并能够得到的重量值.程序Procedure DP_stone1(n:longint);var I,j,total:longint;beginf[0]:=tru
pascal石子归并
石子归并一:给出n堆石子的重量W1,W2.WN,要求你合并其中的任意两堆或者n堆(n>=2),求出所有经过合并能够得到的重量值.
程序
Procedure DP_stone1(n:longint);
var I,j,total:longint;
begin
f[0]:=true;
total:=sum;{所有石子的总和}
for j:=0 to n do
for i:=total-w[i] downto 0 do
if f[i]=true then f[i+w[j]]:=true
end;
我想问一下倒数第二行的f【i】电脑怎么知道是不是正确的,他不是只给了f0的和f(total)的值吗
pascal石子归并 石子归并一:给出n堆石子的重量W1,W2.WN,要求你合并其中的任意两堆或者n堆(n>=2),求出所有经过合并能够得到的重量值.程序Procedure DP_stone1(n:longint);var I,j,total:longint;beginf[0]:=tru
这里用倒推保证是正确的.、
for j:=0 to n do
for i:=total-w[i] downto 0 do
if f[i]=true then f[i+w[j]]:=true
如果写
for j:=0 to n do
for i:=0 to total-w[i] do
if f[i]=true then f[i+w[j]]:=true
就会出现f[i+w[i]]因为f[i]是true,再次循环外层的j时,f[i+w[i]+w[i]]=true的情况(这就是完全背包了,这里不讨论)
至于楼主的问题.摆脱,外层还有个表示j循环呢、.j=k时会求出前k个石子可以合并出哪些