The Balance
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3673 Accepted Submission(s): 1457
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.
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.
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
lcy
题意:给你n个砝码,求出1-所有砝码总重量之间不能用砝码称出的重量,输出不能称出的砝码个数和从小到打输出不能称出的重量
解题思路:
原本用背包做了一次,WA,不过没找出来哪里错了,没想到这次用母函数做,居然AC了Orz......
一直以为用背包做这种问题会比较简单。。。。
#include<stdio.h> int num[101]; int c1[10001],c2[10001]; int main(void) { int n; // freopen("d:\\in.txt","r",stdin); while(scanf("%d",&n)==1) { int i,j,k; int max=0; for(i=1;i<=n;i++) { scanf("%d",&num[i]); max+=num[i]; } for(i=1;i<=max;i++) { c1[i]=0; c2[i]=0; } c1[0]=1; c1[num[1]]=1; for(i=2;i<=n;i++) { for(j=0;j<=max;j++) { if(c1[j]) { c2[j]=1; if(j+num[i]<=max) c2[j+num[i]]=1; } if(j-num[i]>=0 && c1[j]) c2[j-num[i]]=1; if(num[i]-j>=0 && c1[j]) c2[num[i]-j]=1; } for(j=0;j<=max;j++) { c1[j]=c2[j]; c2[j]=0; } } int count=0; for(i=1;i<=max;i++) { if(!c1[i]) count++; } printf("%d\n",count); for(i=1;i<=max;i++) { if(!c1[i]) { printf("%d",i); if(count>1) printf(" "); else printf("\n"); count--; } } } return 0; }