2013
11-08

# 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.

Each line in the input file describes one collection of marbles to be divided. The lines contain six non-negative integers n1 , . . . , 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.

For each collection, 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.

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

Collection #1:
Can't be divided.

Collection #2:
Can be divided.

import java.io.BufferedReader;
import java.io.IOException;

public class Main {

public static void main(String[] args) throws IOException {
System.in));
String[] s;
int[] marbles;
int sum;
int part;
int times = 0;
while (true) {
marbles = new int[6];
sum = 0;
for (int i = 0; i < 6; i++) {
marbles[i] = Integer.parseInt(s[i]);
}
if (marbles[5] > 5) {
marbles[5] = 4 + marbles[5] % 2;
}
if (marbles[4] > 6) {
marbles[4] = 6 - marbles[4] % 2;
}
if (marbles[3] > 5) {
marbles[3] = 4 + marbles[3] % 2;
}
if (marbles[2] > 5) {
marbles[2] = 4 + marbles[2] % 2;
}
if (marbles[1] > 4) {
marbles[1] = 4 - marbles[1] % 2;
}
for (int i = 0; i < 6; i++) {
sum += marbles[i] * (i + 1);
}
if (sum == 0) {
break;
}
times++;
System.out.printf("Collection #%d:\n", times);
if (sum % 2 != 0) {
System.out.println("Can't be divided.");
System.out.println();
continue;
}
part = sum / 2;
if (search(part, marbles)) {
System.out.println("Can be divided.");
} else {
System.out.println("Can't be divided.");
}
System.out.println();
}
}

public static boolean search(int part, int[] marbles) {
int[] flg = new int[part + 1];
flg[0] = 1;
for (int i = 0; i < 6; i++) {
for (int j = 1; j <= marbles[i]; j++) {
int base = j * (i + 1);
if (base > part) {
break;
}
for (int k = part - (i + 1); k >= base - i - 1; k--) {
if (flg[k] != 0) {
flg[k + i + 1] = 1;
}
if (flg[part] != 0) {
return true;
}
}
}
}
return flg[part] != 0;
}

}

1. 在方法1里面：

//遍历所有的边，计算入度
for(int i=0; i<V; i++)
{
degree = 0;