• HDU 1709 The Balance


    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

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <string.h>
    using namespace std;
    int
    main()
    {

        int
    a[10003],r[10003],b[103];//开始WA了,检查了N久,试了好多数据都没问题
        int
    i,k,n,sum;                           //对比了下别人的代码,发现我的r[]写错了10003写成1003,囧
        while
    (scanf("%d",&n)!=EOF)
        {
        sum=0;
            for
    (i=1;i<=n;sum+=b[i],i++)
              scanf("%d",&b[i]);
            memset(a,0,sizeof(a));
            memset(r,0,sizeof(r));
           r[0]=a[0]=1;
           r[b[1]]=a[b[1]]=1;
            for
    (k=2;k<=n;k++)
             {

                 for
    (i=0;i+b[k]<=sum;i++)
                   {

                       a[i+b[k]]+=r[i];
                       a[int(fabs(1.0*i-b[k]))]+=r[i];//关键点
                    }

                    for
    (i=0;i<=sum;i++)
                       r[i]=a[i];
             }

            for
    (k=0,i=1;i<=sum;i++)
              if
    (!a[i])
               {

                   r[k++]=i;
               }

            printf("%d\n",k);
            if
    (k)
            {

              for
    (i=0;i<k-1;i++)
                printf("%d ",r[i]);
                printf("%d\n",r[i]);
            }
        }                    //这题这样做速度挺慢,差点超时, 再去想想其他办法或者优化下。

        return
    0;
    }

    //刚刚去想了下,做出如下改进,快了近一半,而且少开了一个数组

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <string.h>
    using namespace std;
    int main()
    {
        freopen("in.txt","r",stdin);
        int a[10003],r[1003],b;
        int i,k,n,sum;
        while(scanf("%d",&n)!=EOF)
        {    sum=0;
        scanf("%d",&b); sum+=b;
        memset(a,0,sizeof(a));
        memset(r,0,sizeof(r));
        r[0]=a[0]=1;
        r[b]=a[b]=1;
            for(k=2;k<=n;k++)
              { scanf("%d",&b);
                 sum+=b;
                 for(i=0;i+b<=sum;i++)
                   {
                       a[i+b]+=r[i];
                       a[int(fabs(1.0*i-b))]+=r[i];
                    }
                for(i=0;i<=sum;i++)
                    r[i]=a[i];
             }
            for(k=0,i=1;i<=sum;i++)
              if(!a[i])
               {
                   r[k++]=i;
               }
            printf("%d\n",k);
            if(k)
            {
              for(i=0;i<k-1;i++)
                printf("%d ",r[i]);
                printf("%d\n",r[i]);
            }
        }
     return 0;
    }

  • 相关阅读:
    QuartzQuartz定时任务
    jdbc模糊查询、分页查询、联合查询
    PreparedStatement
    web服务器简述
    JDBC基本操作
    RMI
    Http编程
    2020毕业季业务开发宝典
    程序设计流程图
    系统概要框图
  • 原文地址:https://www.cnblogs.com/372465774y/p/2430087.html
Copyright © 2020-2023  润新知