首页 > 代码库 > hdu 1709 母函数变形

hdu 1709 母函数变形

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #define MAX 10010
  5. int c1[MAX], c2[MAX], num[110];
  6. /* 题目大意: 给你n种砝码,从1到n中砝码的重量和其中不能称出多少种重量,输出不能称出的总数和类别*/
  7. int main()
  8. {
  9. int n;
  10. while( scanf("%d", &n)!=EOF )
  11. {
  12. memset(num, 0, sizeof(num) );
  13. memset(c1, 0, sizeof(c1) );
  14. memset(c2, 0, sizeof(c2) );
  15. int total = 0;
  16. for(int i=0; i<n; i++) //重量输入
  17. {scanf("%d", &num[i]); total+=num[i];}
  18. //for(int i=0; i<=total; i++)
  19. //c1[i] = c2[i] = 0;
  20. c1[0] = 1; //第一个括号,重量为0的
  21. for(int i=0; i<n; i++)
  22. {
  23. for(int j=0; j<=total; j++)
  24. {
  25. c2[j] |= c1[j]; //否能称得,自己砝码的重量
  26. if(num[i] + j <=total) //重量和是否大于总重量
  27. c2[j+num[i]] |= c1[j]; //重量和
  28. c2[abs(j-num[i])] |= c1[j]; //重量差
  29. }
  30. for(int j=0; j<=total; j++) //一次循环赋值
  31. {c1[j] = c2[j]; c2[j] = 0;}
  32. }
  33. int count = 0;
  34. for(int i=0; i<=total; i++) //查找能否称得的重量
  35. {
  36. if(!c1[i])
  37. c2[count++] = i;
  38. }
  39. printf("%d\n", count);
  40. for(int i=0; i<count-1; i++)
  41. printf("%d ", c2[i]);
  42. if(count) printf("%d\n", c2[count-1]);
  43. }
  44. return 0;
  45. }
  46. /*
  47. Problem Description
  48. 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.
  49. Input
  50. 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.
  51. Output
  52. 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.
  53. Sample Input
  54. 3
  55. 1 2 4
  56. 3
  57. 9 2 1
  58. Sample Output
  59. 0
  60. 2
  61. 4 5
  62. */



来自为知笔记(Wiz)


附件列表

     

    hdu 1709 母函数变形