首页 > 专题系列 > Java解POJ > POJ 1014 Dividing [解题报告] Java
2013
11-08

POJ 1014 Dividing [解题报告] Java

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; 
import java.io.InputStreamReader; 

public class Main { 

    public static void main(String[] args) throws IOException { 
        BufferedReader read = new BufferedReader(new InputStreamReader( 
                System.in)); 
        String[] s; 
        int[] marbles; 
        int sum; 
        int part; 
        int times = 0; 
        while (true) { 
            marbles = new int[6]; 
            sum = 0; 
            s = read.readLine().split(" "); 
            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;
    for (j = adj .begin(); j != adj .end(); ++j)
    {
    degree[*j]++;
    }
    }

    为什么每遍历一条链表,要首先将每个链表头的顶点的入度置为0呢?
    比如顶点5,若在顶点1、2、3、4的链表中出现过顶点5,那么要增加顶点5的入度,但是在遍历顶点5的链表时,又将顶点5的入度置为0了,那之前的从顶点1234到顶点5的边不是都没了吗?

  2. 第二块代码if(it != mp.end())应改为if(it != mp.end() && (i+1)!=(it->second +1));因为第二种解法如果数组有重复元素 就不正确

  3. 很高兴你会喜欢这个网站。目前还没有一个开发团队,网站是我一个人在维护,都是用的开源系统,也没有太多需要开发的部分,主要是内容整理。非常感谢你的关注。