C - Dividing
来源:互联网 发布:东南大学翻译硕士 知乎 编辑:程序博客网 时间:2024/06/05 10:39
C - Dividing
Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value.
Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.
Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.
The last line of the input file will be ``0 0 0 0 0 0''; do not process this line.
Output a blank line after each test case.
1 0 1 2 0 01 0 0 0 1 10 0 0 0 0 0
Collection #1:Can't be divided.Collection #2:Can be divided.
注意:这一题太坑了,要用到二进制的思想,但我还不是太会,请看下面代码
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int a[8];int dp[200009];int shu[200009];int sum;int main(){ int flag=1; while(1) { sum=0; memset(dp,0,sizeof(dp)); memset(shu,0,sizeof(shu)); for(int i=1; i<=6; i++) { scanf("%d",&a[i]); sum+=a[i]*i; } if(sum==0) break; printf("Collection #%d:\012",flag++); if(sum/2*2!=sum)//和为奇数时,一定不能均分 printf("Can't be divided.\012\012"); else { int i,j,k,flg=0; int total=0; for(i = 1; i <=6; i++)//将每个数分成几个小数,使任意个小数相加为他本身 { if(a[i]>0) { for(int j=1; j<a[i]; j<<=1) { shu[total++]=j*i; a[i]-=j; } shu[total++] = a[i]*i; } } dp[0]=1; for(i=0; i<total; i++)//零一背包 { for(k=sum/2; k>=shu[i]; k--) { if(dp[k-shu[i]]!=0) dp[k]=max(dp[k-shu[i]]+shu[i],dp[k]); if(dp[sum/2]) flg=1; } if(flg) break; } if(flg) printf("Can be divided.\n\n"); else printf("Can't be divided.\012\012"); } } return 0;}
0 0
- C - Dividing
- Dividing
- Dividing
- Dividing
- Dividing
- Dividing
- Dividing
- Dividing
- Dividing
- Dividing
- codeforces——C. Dividing the numbers
- codeforces Div.2 899C Dividing the numbers
- 1014 Dividing
- POJ Dividing
- HDUOJ1059 Dividing
- hdu1059 Dividing
- poj1014 - Dividing
- poj1014:Dividing
- 状态模式(python)
- 项目记录,Mybatis框架下批量修改数据库,你要绕过这些坑
- CentOS下解压.rar格式文件
- 怦然心动
- html文件结构
- C - Dividing
- 删除指定目录下的文件(不包括次目录)
- js或jquery实现页面打印可局部打印
- 访问者模式(python)
- Qt中两个窗口相互操作
- [BZOJ1923][Sdoi2010]外星千足虫(高斯消元)
- java.util.ConcurrentModificationException
- ==与===
- Python pip 国内镜像大全及使用办法