zoukankan      html  css  js  c++  java
  • The Balance

    The Balance

    Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 135 Accepted Submission(s): 88
     
    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
     
    Recommend
    lcy
     
    /*
    题意:给出n个砝码的质量,让你求出在[1,s]范围内不能组成的质量,s是质量总和,这个题不一样的地方是天平两边都可以放砝码
    
    初步思路:可以看成一个背包问题,每个取或不取,但是这个题有一个新的状态,就是
    */
    #include<bits/stdc++.h>
    using namespace std;
    int v[10010];
    int dp[10010];//dp[i]表示i金额能组成或不能组成
    int vis[10010];//记录一下能组成的金额
    int n;
    int s=0;
    void init(){
        memset(vis,0,sizeof vis);
        memset(dp,0,sizeof dp);
        s=0;
    }
    int main(){
        // freopen("in.txt","r",stdin);
        while(scanf("%d",&n)!=EOF){
            init();
            for(int i=1;i<=n;i++){
                scanf("%d",&v[i]);
                s+=v[i];
            }
            vis[0]=1;
            for(int i=1;i<=n;i++){
                memset(dp,0,sizeof dp);
                for(int j=0;j<=s;j++){
                    if(vis[j]){//j金额能组成
                        dp[j+v[i]]=1;//加上当前金额那么也是可以的
                        dp[abs(j-v[i])]=1;//减去这个金额的也是可以的
                    }
                }
                for(int j=0;j<=s;j++){
                    if(dp[j]) vis[j]=1;
                }
            }
            int res=0;
            for(int i=1;i<=s;i++)
                if(!vis[i]) 
                    res++;
            printf("%d
    ",res);
            if(res==0) continue;
            int f=0;
            for(int i=1;i<=s;i++){
                if(!vis[i]){
                    if(f==0){
                        printf("%d",i);
                        f=1;
                    }else{
                        printf(" %d",i);
                    }
                }
            }
            printf("
    ");
        }
        return 0;
        
    }
  • 相关阅读:
    JVM系列(三)垃圾回收
    JVM系列(二)各区域的OOM
    JVM系列(一)内存模型
    获取IP、mac等信息
    memcached(七)--常用指令抓包分析
    memcached(五)--源码分析,启动
    apt安装mysql + 简易配置
    memcached(六)--spyMemcached的get操作
    解决mysql获取不到连接的问题
    tcp一些要点
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6404115.html
Copyright © 2011-2022 走看看