UVa 10664

an easy 0-1 knapsack problem
strtok and gets are used to parse input...
/* UVas */
/*****************************************************************************/
#define N 21
#define S 101
#define BUF 512
int m;
int l[N];
int d[S];
char str[BUF];
int main(void) {
    scanf("%d", &m);
    gets(str);
    while (m--) {
        gets(str);
        int i, s=0, idx=0;
        for (char *ptr=strtok(str, " "); ptr; ptr=strtok(0, " ")) {
            i=atoi(ptr), l[idx++]=i, s+=i;
        }
        if (s%2) {
            printf("NO\n");
            continue;
        }
        s/=2;
        memset(d, 0, sizeof(d));
        d[0]=1;
        for (int i=0; i<idx; i++)
            for (int j=S-1; j>=l[i]; j--)
                d[j]|=d[j-l[i]];
        d[s]?printf("YES\n"):printf("NO\n");
    }
}
/*****************************************************************************/

No comments :

Post a Comment