ZOJ 1149 Dividing(穷举)

来源:互联网 发布:如何延长淘宝付款时间 编辑:程序博客网 时间:2024/06/10 11:29

Dividing

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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.


Input 

Each line in the input describes one collection of marbles to be divided. The lines consist of six non-negative integers n1, n2, ..., n6, where ni is the number of marbles of value i. So, the example from above would be described by the input-line ``1 0 1 2 0 0''. The maximum total number of marbles will be 20000.

The last line of the input file will be ``0 0 0 0 0 0''; do not process this line.


Output 

For each colletcion, output ``Collection #k:'', where k is the number of the test case, and then either ``Can be divided.'' or ``Can't be divided.''.

Output a blank line after each test case.


Sample Input

1 0 1 2 0 0
1 0 0 0 1 1
0 0 0 0 0 0


Sample Output

Collection #1:
Can't be divided.

Collection #2:
Can be divided.


Source: Mid-Central European Regional Contest 1999


思路:预处理输入的大理石的数目,然后枚举即可。


AC代码:

#include<stdio.h>int a[7];void enumerate(int sum) {int mid;int i1,i2,i3,i4,i5,i6;for(i1=0; i1<=a[1]; i1++)for(i2=0; i2<=a[2]; i2++)for(i3=0; i3<=a[3]; i3++)for(i4=0; i4<=a[4]; i4++)for(i5=0; i5<=a[5]; i5++)for(i6=0; i6<=a[6]; i6++){mid = 1*i1+2*i2+3*i3+4*i4+5*i5+6*i6;if(mid==sum/2) {printf("Can be divided.\n\n");return;}}printf("Can't be divided.\n\n");}int main(){int i;int iCase = 1;while(1){int sum = 0;for(i=1; i<=6; i++){scanf("%d",&a[i]);//数据预处理 if(a[i]!=0 && a[i]%6==0) a[i] = 6;else a[i] = a[i]%6;sum += a[i]*i;//预处理之后大理石的总价值}if (sum==0) break;printf("Collection #%d:\n",iCase++);if (sum%2==1) {printf("Can't be divided.\n\n");continue;}enumerate(sum);}return 0;}