题解 | #称砝码#
称砝码
http://www.nowcoder.com/practice/f9a4c19050fc477e9e27eb75f3bfd49c
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n;
int cnt = 1;
scanf("%d", &n);
int *weight = (int *)malloc(sizeof(int) * n);
int *num = (int *)malloc(sizeof(int) * n);
for(int i=0; i<n; i++)
{
scanf("%d ", weight+i);
}
for(int i=0; i<n; i++)
{
scanf("%d ", num+i);
cnt += weight[i] * num[i];
}
int *sum = (int *)malloc(sizeof(int) * cnt);
sum[0] = 1;
for(int i=0; i<n; i++)
{
for(int j=0; j<num[i]; j++)
{
for(int k=cnt-1; k>=0; k--)
{
if(sum[k])
sum[k + weight[i]] = 1;
}
}
}
int count = 0;
for(int i=0; i<cnt; i++)
{
if(sum[i])
count++;
}
printf("%d\n", count);
free(weight);
weight = NULL;
free(num);
num = NULL;
free(sum);
sum = NULL;
return 0;
}