本文主要是介绍hdu 1059 背包,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#include<cstdio>
#include<cstring>
#define MIN(x,y) ((x)>(y)?(y):(x))
#define MAX(x,y) ((x)>(y)?(x):(y))
int dp[42000];
int n[8];
int main()
{int s=1;while(~scanf("%d%d%d%d%d%d",&n[1],&n[2],&n[3],&n[4],&n[5],&n[6])){int sum;if((n[1]+n[2]+n[3]+n[4]+n[5]+n[6])==0)break;sum=1*n[1]+2*n[2]+3*n[3]+4*n[4]+5*n[5]+6*n[6];// printf("sum=%d\n",sum);if(sum%2)printf("Collection #%d:\nCan't be divided.\n\n",s++); else{for(int i=0;i<=sum/2;i++)dp[i]=0;for(int i=1;i<=6;i++){int num=n[i];for(int k=1;num>0;k<<=1){int mul=MIN(k,num);for(int j=sum/2;j>=i*mul;j--)dp[j]=MAX(dp[j],dp[j-i*mul]+i*mul);num-=mul;}}if(dp[sum/2]==sum/2)printf("Collection #%d:\nCan be divided.\n\n",s++);elseprintf("Collection #%d:\nCan't be divided.\n\n",s++); }}
}
这篇关于hdu 1059 背包的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!