笨笨猪 @ 2017-01-13 12:45:51
作为一名写C++的学生在做题时发现了题解中有一篇Pascal代码中使用了十分巧妙的位运算方法来给主件和附件分组,于是模仿着使用c++进行编写。
可是十分曲折,调试了很久提交了n次还是不过,很生气!
于是气急败坏的把pascal交了上去,ac了,然后把c++的.一模一样的代码(连变量名都一样!)交了上去,0分!!(毕竟连样例都过不了)
自己和同学加起来核对了十多遍,真心不知道为什么?
求大神解答!谢谢各位!【有图有真相】
#include<cstdio>
#include<algorithm>
using namespace std;
int i,j,k,m,n,x,t,ww,vv,w[100010],v[100010],a[1000][10],f[100010],cap;
bool boo[100010];
int main()
{
scanf("%d%d",&cap,&n);
for (i=1;i<=n;++i)
{
scanf("%d%d%d",&w[i],&v[i],&x);
v[i]=v[i]*w[i];
if (x==0) continue;
boo[i]=true;
++a[x][0];
a[x][a[x][0]]=i;
}
for (i=1;i<=n;++i)
if (!boo[i])
for (j=cap;j>=w[i];--j)
{
for (k=0;k<=(1<<a[i][0]-1);++k)
{
ww=0;
vv=0;
for (t=1;t<=a[i][0];++t)
if (((k>>(t-1))and 1)==1)
{
ww+=w[a[i][t]];
vv+=v[a[i][t]];
}
if (j>=ww+w[i]) f[j]=max(f[j],f[j-ww-w[i]]+vv+v[i]);
}
}
printf("%d\n",f[cap]);
return 0;
}
然后是题解中的Pascal代码
var
i,j,t,k,x,ww,vv,n,cap:longint;
w,v,f:array[0..100010] of longint;
a:array[0..1000,0..10] of longint;
boo:array[0..100010] of boolean;
function max(x,y:longint):longint;
begin
if x>y then exit(x);exit(y);
end;
begin
readln(cap,n);
for i:=1 to n do
begin
readln(w[i],v[i],x);
v[i]:=v[i]*w[i];
if x=0 then continue;
boo[i]:=true;
inc(a[x,0]);a[x,a[x,0]]:=i;
end;
for i:=1 to n do
if not boo[i] then
for j:=cap downto w[i] do
begin
for k:=0 to 1<<a[i,0]-1 do
begin
ww:=0;vv:=0;
for t:=1 to a[i,0] do
if (k>>(t-1))and 1=1 then
begin inc(ww,w[a[i,t]]);inc(vv,v[a[i,t]]);end;
if j>=ww+w[i] then f[j]:=max(f[j],f[j-ww-w[i]]+vv+v[i]);
end;
end;
writeln(f[cap]);
end.
求教了!
by 小鸽子 @ 2017-03-19 08:21:10
uc是啥…………
by LzzAndHzz @ 2017-03-25 09:48:32
啪啪啪