/*The Balance Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5541 Accepted Submission(s): 2243 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 HDU 2007-Spring Programming Contest */ #include<stdio.h> #include<string.h> int main() { int a[110], b[11000], i, j, k, n, sum, c[11000], lc; while(scanf("%d", &n) != EOF) { sum = 0; memset(b, 0 , sizeof(b)); for(i = 0; i < n; i++) { scanf("%d", &a[i]); sum += a[i]; } b[0] = 1;//初始化 b[a[0]] = 1;//初始化 for(i = 2; i <= n; i++) { memset(c, 0, sizeof(c)); for(j = 0; j <= sum; j++) for(k = 0; k + j <= sum && k <= a[i-1]; k += a[i-1])//每个重量的砝码只有一个 { if( k > j) c[k-j] += b[j];//双向称砝码 else c[j-k] += b[j]; c[j+k] += b[j]; } for(k =0; k <= sum ; k++) b[k] = c[k]; } int num = 0; for(i = 0;i <= sum; i++) if( !b[i]) num++; printf("%d\n", num); if(num == 0) continue; else { j = 0; for(i = 0; i <= sum; i++) if(!b[i]) { j++; if(j != num) printf("%d ", i); else printf("%d\n", i); } } } return 0; }
题意:给你n个砝码,用天秤秤出所能得到的各种质量,输出1到砝码总和之间所不能得到的数字。
注意:砝码可以左右分开放,得到不同的质量数。
这里用的依旧是母函数,难点在于怎么处理左右放砝码,左右放砝码就好比是减法得出一个新的数字。