poj1014

题意:就是从1到6给你每个数有几个,然后问你这些数能平均分给 两个人么

刚看到这个题,直接背包    这就是背包九讲中说的多重背包,

但是我用母函数解决起来也非常快,思路就是看看中间值系数是否为0就可以了

下面看代码:

#include
#include
#include
#include


using namespace std;

int c1[200005],c2[200005];

int main()

{
    int i,j,k,t;
    int f[9];
    int cas = 1;
    while(1)
    {
        int sum = 0;
        for(i = 1;i <= 6;i++)
        {
            scanf("%d",&f[i]);
            if(f[i] >= 8)
            {
                if(f[i]%2)
                {
                    f[i] = 11;
                }
                else
                    f[i] = 12;
            }
            sum += f[i] * i;
        }
        memset(c1,0,sizeof(c1));
        memset(c2,0,sizeof(c2));
        c1[0] = 1;
        t = sum / 2;
        if(sum == 0)
            break;
        if(sum % 2 == 1)
        {
            printf("Collection #%d:\n",cas++);
            printf("Can't be divided.\n");
            printf("\n");
        }
        else
        {
            for(i = 1;i <= 6;i++)
            {
                for(j = 0;j <= t;j++)
                {
                    for(k = 0;k <= f[i]&&k * i + j <= t;k++)
                    {
                        c2[k * i + j] += c1[j];
                    }
                }
                for(j = 0;j <= t;j++)
                {
                    c1[j] = c2[j];
                    c2[j] = 0;
                }
            }
            //printf("c1[t] = %d\n",c1[t]);
            if(c1[t] == 0)
            {
                printf("Collection #%d:\n",cas++);
                printf("Can't be divided.\n");
                printf("\n");
            }
            else
            {
                printf("Collection #%d:\n",cas++);
                printf("Can be divided.\n");
                printf("\n");
            }
        }
    }
}


你可能感兴趣的:(算法)