The Balance
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6652 Accepted Submission(s): 2730
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
Source
Recommend
#include<stdio.h> #include<string.h> #include<math.h> int num[10005]; int ans[10005]; int c[10005],temp[10005]; int main(){ int n; while(scanf("%d",&n)!=EOF){ memset(num,0,sizeof(num)); memset(c,0,sizeof(c)); memset(ans,0,sizeof(ans)); memset(temp,0,sizeof(temp)); int total=0; for(int i=0;i<n;i++){ scanf("%d",&num[i]); total+=num[i]; } for(int i=0;i<=num[0];i+=num[0]) c[i]=1; for(int i=1;i<n;i++){ for(int j=0;j<=total;j++){ for(int k=0;k+j<=total&&k/num[i]<=1;k+=num[i]){///注意《=1 temp[j+k]+=c[j]; temp[(int)fabs(j-k)]+=c[j];//注意此刻应当考虑负值的情况 } } for(int ii=0;ii<=total;ii++) c[ii]=temp[ii]; } int cnt=0; for(int i=1;i<=total;i++){ if(!c[i]){ cnt++; ans[cnt]=i; } } printf("%d ",cnt); for(int i=1;i<=cnt;i++){ printf("%d%c",ans[i],i==cnt?' ':' '); } } return 0; }