版权声明:转载请注明出处。 https://blog.csdn.net/u014427196/article/details/44496867

Divideing Jewels
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 164 Solved: 22
[Submit][Status][Web Board]
Description
Mary and Rose own a collection of jewells. They want to split the collection
among themselves so that both receive an equal share of the jewels. This would
be easy if all the jewels had the same value, because then they could just
split the collection in half. But unfortunately, some of the jewels are
larger, or more beautiful than others. So, Mary and Rose start by assigning a
value, a natural number between one and ten, to each jewel. Now they want to
divide the jewels so that each of them gets the same total value.
Unfortunately, they realize that it might be impossible to divide the jewels
in this way (even if the total value of all jewels is even). For example, if
there are one jewel 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 jewels.

Input
Each line in the input file describes one collection of jewels to be divided.
The lines contain ten non-negative integers n1 , … , n10 , where ni is the
number of jewels of value i. The maximum total number of jewells will be

  1. The last line of the input file will be “0 0 0 0 0 0 0 0 0 0”; do not process
    this line.

Output
For each collection, output “#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(Except the last case).

Sample Input
1 0 1 2 0 0 0 0 2 0
1 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Sample Output

#1:Can't be divided.
#2:Can be divided.

dp:

#include <stdio.h>  
#include <iostream>  
#include <math.h>  
#include <stdlib.h>  
#include <ctype.h>  
#include <algorithm>  
#include <vector>  
#include <string.h>  
#include <queue>  
#include <stack>  
#include <set>  
#include <map>  
#include <sstream>  
#include <time.h>  
#include <malloc.h>  

using namespace std;

const int MAXN = 100000;

int p[MAXN],dp[MAXN];

int vis[MAXN], tmp, ave;

int main()
{
    int cases = 1;
    while (1)
    {
        memset(vis, 0, sizeof(vis));
        int ok = 0;
        int sum = 0;
        for (int i = 1; i <= 10; i++)
        {
            scanf("%d",&p[i]);
            sum += i * p[i];
            if (p[i] != 0) ok = 1;
        }
        p[0] = 0;

        if (!ok) break;
        if (cases != 1) printf("\n");

        if (sum % 2 != 0)
        {
            printf("#%d:Can't be divided.\n", cases++);
            continue;
        }
        else
        {
            ave = sum / 2;
            memset(dp, -1, sizeof(dp));

            dp[0] = 0;

            for (int i = 0; i <= 10; i++)
            {
                for (int j = 0; j <= ave; j++)
                {
                    if (dp[j] >= 0)
                        dp[j] = p[i];
                    else if (j < i || dp[j - i] <= 0)
                    {
                        dp[j] = -1;
                    }
                    else
                    {
                        dp[j] = dp[j - i] - 1;
                    }
                }
            }

            if (dp[ave]>=0)
                printf("#%d:Can be divided.\n", cases++);
            else
                printf("#%d:Can't be divided.\n", cases++);
        }
    }
    return 0;
}

dfs:

#include <stdio.h>  
#include <iostream>  
#include <math.h>  
#include <stdlib.h>  
#include <ctype.h>  
#include <algorithm>  
#include <vector>  
#include <string.h>  
#include <queue>  
#include <stack>  
#include <set>  
#include <map>  
#include <sstream>  
#include <time.h>  
#include <malloc.h>  

using namespace std;

const int MAXN = 1000000;

int p[MAXN];

int val[MAXN], tmp, ave;

int dfs(int sum,int num)
{
    if (sum == 0) return 1;
    if (sum < 0 || (sum>0 && num == 0)) return 0;
    if (dfs(sum - val[num], num - 1)) return 1;
    dfs(sum,num-1);
}

int main()
{
    int cases = 1;
    while (1)
    {
        int sum = 0;
        int len = 1;
        for (int i = 1; i <= 10; i++)
        {
            scanf("%d", &p[i]);
                sum += i * p[i];
            for (int j = 0; j < p[i]; j++)
                val[len++] = i;
        }

        if (sum==0) break;
        if (cases != 1) printf("\n");

        if (sum % 2 != 0)
        {
            printf("#%d:Can't be divided.\n", cases++);
            continue;
        }
        if (dfs(sum/2,len-1))
            printf("#%d:Can be divided.\n", cases++);
        else
            printf("#%d:Can't be divided.\n", cases++);
    }
    return 0;
}