您的位置:首页 > 编程语言 > C语言/C++

HDU 1059 Dividing(多重背包)

2014-08-27 20:26 405 查看
题目地址:HDU 1059

这题在看多重背包以前碰到过。。现在觉得好水。。。本来觉得不用写在博客里的。。但是发现今天还一篇也没写。。。还是写一篇吧。。。

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

using namespace std;
int b[100000], dp[100000];
int main()
{
int a[10], i, sum, j, k, num=0, cnt, x, y;
while(scanf("%d%d%d%d%d%d",&a[0],&a[1],&a[2],&a[3],&a[4],&a[5])!=EOF&&(a[0]+a[1]+a[2]+a[3]+a[4]+a[5]))
{
num++;
cnt=0;
sum=0;
for(i=0; i<6; i++)
sum+=a[i]*(i+1);
printf("Collection #%d:\n",num);
if(sum%2)
{
printf("Can't be divided.\n\n");
continue ;
}
sum/=2;
for(i=0;i<6;i++)
{
x=a[i];
y=1;
while(x)
{
if(x<=y)
{
b[cnt++]=(i+1)*x;
break;
}
b[cnt++]=(i+1)*y;
x-=y;
y*=2;
}
}
memset(dp,0,sizeof(dp));
dp[0]=1;
for(i=0;i<cnt;i++)
{
for(j=sum;j>=b[i];j--)
{
if(dp[j-b[i]])
{
dp[j]=1;
}
}
}
if(dp[sum])
printf("Can be divided.\n");
else
printf("Can't be divided.\n");
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息