Problem Description
Now you are asked to measure a dose of medicine with a balance and a number of weights. Certainly it is not always achievable. So you should find out the qualities which cannot be measured from the range [1,S]. S is the total quality of all the weights.
Input
The input consists of multiple test cases, and each case begins with a single positive integer N (1<=N<=100) on a line by itself indicating the number of weights you have. Followed by N integers Ai (1<=i<=N), indicating the quality of each weight where 1<=Ai<=100.
Output
For each input set, you should first print a line specifying the number of qualities which cannot be measured. Then print another line which consists all the irrealizable qualities if the number is not zero.
Sample Input
3
1 2 4
3
9 2 1
Sample Output
0
2
4 5
题意: 输入砝码的个数 之后输入砝码的各个质量 看从1->砝码的总和 这些质量中 那些质量测不出来 砝码可以放在天平的2边
#include<stdio.h> #include<math.h> #include<string.h> int c1[10010],c2[10010],val[105],ans[10010]; int main() { int n,i,j,k,max,cnt; while(scanf("%d",&n)!=EOF) { max=0; for(i=1;i<=n;i++) { scanf("%d",&val[i]); max+=val[i]; } memset(c1,0,sizeof(c1)); memset(c2,0,sizeof(c2)); // for(i=0;i<max;i++) 注意 for(i=0;i<=val[1];i+=val[1])//是小于val[1] 因为砝码只有一个 最大为val[1]*1以前是由于砝码的数量不受限制才会小于max { c1[i]=1; } for(i=2;i<=n;i++) { for(j=0;j<=max;j++) for(k=0;k+j<=max&&k<=val[i];k=k+val[i])//k<=val[i]其实是小于 val[i]*1 因为砝码只有一个!!! { c2[k+j]+=c1[j]; c2[abs(k-j)]+=c1[j];//这个地方也是加 因为存的是系数 也就是是方案数 } for(k=0;k<=max;k++) { c1[k]=c2[k]; c2[k]=0; } } cnt=0; for(i=1;i<=max;i++) { if(!c1[i]) ans[cnt++]=i; } if(cnt==0) {printf("0\n");continue;} printf("%d\n",cnt); for(i=0;i<cnt-1;i++) printf("%d ",ans[i]); printf("%d\n",ans[cnt-1]); } }