• HDU 1709 The Balance


    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
     
    题意:
    题意:给定n个砝码的重量,总质量为sum,问在1~sum中有多少个重量不能被称出来。
    每种砝码既可以放在右盘,又可以放在左盘,(若按左物右码来说),放在左盘那就取减号,放在右盘就取加号。
     
    就是套用01背包的代码
     方程:
                   c[j] |= b[j];//c【】,b【】,有一个为1 就成立,不加a【】的情况
                   if(j+a[i]<=sum)
                       c[j+a[i]]+=b[j];//加a【】
                   c[(int)abs(j-a[i])]+=b[j];//两个砝码的差
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    using namespace std;
    int main()
    {
       int t,a[105],i,j,sum;
       int b[10005],c[10005];
       while(scanf("%d",&t)!=-1)
       {
           sum=0;
           for(i=1;i<=t;i++)
           {
               scanf("%d",&a[i]);
               sum+=a[i];
           }
           memset(b,0,sizeof(b));
           memset(c,0,sizeof(c));
           b[0]=1;
           b[a[1]]=1;
           for(i=2;i<=t;i++)
           {
               for(j=0;j<=sum;j++)
               {
                   c[j] |= b[j];//c【】,b【】,有一个为1 就成立,不加a【】的情况
                   if(j+a[i]<=sum)
                       c[j+a[i]]+=b[j];//加a【】
                   c[(int)abs(j-a[i])]+=b[j];//两个砝码的差
               }
               memcpy(b,c,sizeof(c));
               memset(c,0,sizeof(c));
           }
           j=0;
           for(i=1;i<=sum;i++)
               if(b[i]==0)
                   c[j++]=i;
               printf("%d
    ",j);
               if(j)
               {
                   printf("%d",c[0]);
                   for(i=1;i<j;i++)
                       printf(" %d",c[i]);
                   printf("
    ");
               }
       }
       return 0;
    }
    View Code
  • 相关阅读:
    rocketmq的linux搭建环境
    linux调用本地shell脚本
    第二课
    第一课
    有意思的dos攻击
    信息收集
    url跳转挖掘
    Drozer之android_app分析实战
    缓存中毒
    随便写写
  • 原文地址:https://www.cnblogs.com/demodemo/p/4732591.html
Copyright © 2020-2023  润新知