c++翻译成pascal

#include <stdio.h>

#include <algorithm>

#include <string.h>

using namespace std;

int dp[100000];

int main()

{

int a[11],sum,v,i,j,k,cnt,cas = 1;

while(~scanf("%d",&a[1]))

{

sum = a[1];

for(i = 2;i<=6;i++)

{

scanf("%d",&a[i]);

sum+=i*a[i];

}

if(!sum)

break;

printf("Collection #%d:\n",cas++);

if(sum%2)//总和为奇数,必定不能平分

{

printf("Can't be divided.\n\n");

continue;

}

v = sum/2;

memset(dp,0,sizeof(dp));

dp[0] = 1;

for(i = 1;i<=6;i++)

{

if(!a[i])

continue;

for(j = 1;j<=a[i];j*=2)//二进制优化

{

cnt = j*i;

for(k = v;k>=cnt;k--)

{

if(dp[k-cnt])//必须前面的能够放入背包,现在的才能放入背包

dp[k] = 1;

}

a[i]-=j;

}

cnt = a[i]*i;//剩下的

if(cnt)

{

for(k = v;k>=cnt;k--)

{

if(dp[k-cnt])

dp[k] = 1;

}

}

}

if(dp[v])

printf("Can be divided.\n\n");

else

printf("Can't be divided.\n\n");

}

return 0;

}
谢谢!
急求!!各位大佬能回答一下吗

蝴蝶刀刀
浏览 832回答 2
2回答

慕码人2483693

var dp:array[0..100000]of longint;&nbsp; &nbsp; a:array[0..11]of longint;&nbsp; &nbsp; sum,i,v,j,k,cnt,casbegin&nbsp; &nbsp; cas:=1;&nbsp; &nbsp; while (not eoln) do&nbsp; &nbsp; begin&nbsp;&nbsp; &nbsp; &nbsp; read(a[1]);&nbsp; &nbsp; &nbsp; sum:=a[1];&nbsp; &nbsp; &nbsp; for i:=2 to 6 do&nbsp;&nbsp; &nbsp; &nbsp; begin&nbsp; &nbsp; &nbsp; &nbsp; read(a[i]);&nbsp; &nbsp; &nbsp; &nbsp; sum:=sum+i*a[i];&nbsp; &nbsp; &nbsp; end;&nbsp; &nbsp; &nbsp; if (sum=0) break;&nbsp; &nbsp; &nbsp; writeln("Collection #%d:",cas++);&nbsp; &nbsp; &nbsp; if(sum mod 2=1)&nbsp; &nbsp; &nbsp; begin&nbsp; &nbsp; &nbsp; &nbsp; writeln("Can't be divided.");&nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; end;&nbsp; &nbsp; &nbsp; v:=sum div 2;&nbsp; &nbsp; &nbsp; fillchar(dp,0,sizeof(dp));&nbsp; &nbsp; &nbsp; dp[0]:=1;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; for i:=1 to 6 do&nbsp;&nbsp; &nbsp; &nbsp; begin&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if(a[i]=0) continue;&nbsp; &nbsp; &nbsp; &nbsp; j=1;&nbsp; &nbsp; &nbsp; &nbsp; while (j<=a[i]) do&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; begin&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cnt:=j*i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for k:=v downto cnt do&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(dp[k-cnt]) dp[k] = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a[i]=a[i]-j;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j:=j*2;&nbsp; &nbsp; &nbsp; &nbsp; end;&nbsp; &nbsp; &nbsp; &nbsp; cnt:=a[i]*i;&nbsp; &nbsp; &nbsp; &nbsp; if (cnt=1)&nbsp; &nbsp; &nbsp; &nbsp; begin&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for k:=v downto cnt do&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (dp[k-cnt]=1) dp[k]=1;&nbsp; &nbsp; &nbsp; &nbsp; end;&nbsp; &nbsp; &nbsp; end;&nbsp; &nbsp; &nbsp; if (dp[v]=1) writeln("Can be divided.")&nbsp; &nbsp; &nbsp; else writeln("Can't be divided.\n\n");&nbsp; &nbsp; end;end.自己改一下吧,没有编译器,sorry了,不会的问我

一只萌萌小番薯

动态规划背包问题吗?可以啊
打开App,查看更多内容
随时随地看视频慕课网APP