HDU 1709 The Balance

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
题目大意:
有一个天平,给你N个质量不同的砝码,求出有哪几个值的质量不能被秤出,
简化一下就是给你N个数,使用这N个数进行加减运算,求出1~N个数之和这个范围内哪些数不能被计算得出。
输出:
如果都能得到就输出0,若不能,输出不能得到的数的个数,并写出具体的值;
思路:
如果一次性拿到所有的数,你会发现会存在很多种组合方法,显得无从下手,那么应该简化一下问题,如果给你一个数,会有几种情况,那答案是显而易见的,只有一种,就是它本身,那么在一个的基础上再给你一个数,又会多几种情况呢,你可以两个数相加,或相减,得到两种情况,再考虑在两个的基础上又得到一个数,会有什么样的变化。
值得注意的是:
一、重复情况,比如我第一个得到的数是1,第二个得到的数是2,那么2-1和1是重复的,由于数据最大范围只在10000,所以可以开book[10000]的数组,用1代表可以得到,0代表不可以得到,来记录每个的值。
二、异步刷新的问题,例如当前可计算出的是为2,7两个数,得到新的数为1,可以新增结果1,3,6,8,若把新得到的数马上加到book表中就会产生错误,1+3得到4,所以应将新增的结果保存在新的数组中,然后再插入到book表中
代码如下:
#include<stdio.h>
#include<algorithm>
using namespace std;
int book[10001];//查询某个数字是否已经可以得到
int visited[10001];//记录可得到的数字
int add[20001];//记录新增可得到的数字
int main()
{
    int n,i,t,vcount,add_count,sum,first;
    while(scanf("%d",&n)!=EOF)
    {
        first=1;//用来控制输出的间隔
        sum=0;
        vcount=0;
        memset(book,0,sizeof(book));
        for(i=0;i<n;i++)
        {
            scanf("%d",&t);
            sum+=t;
            add_count=0;//初始化新增可得到的数字为0个
            add[add_count++]=t;//数字自身一定可以得到
            for(int p=0;p<vcount;p++)//遍历visited表,再之前的基础上,新的数能得到其他数
            {
                add[add_count++]=visited[p]+t;//和其他可得到的数相加
                if(t<visited[p])//和其他可得到的数相减
                add[add_count++]=visited[p]-t;
                else if (t>visited[p])
                add[add_count++]=t-visited[p];
            }
            //由于以上操作会有重复的情况产生,所以要查询book表,剔除已有数据
            for(int p=0;p<add_count;p++)
            {
                if(book[add[p]]==0)//若之前不能得到,那么就插入到visited表中,并将book表中置1,表示已经可以得到
                {
                    book[add[p]]=1;
                    visited[vcount++]=add[p];
                }
            }
        }
        printf("%d\n",sum-vcount);
        if(sum-vcount!=0)
        {
            for(int p=1;p<sum;p++)
            {
                if(book[p]==0)
                {
                    if(first)
                        first=0;
                    else
                        printf(" ");
                    printf("%d",p);
                }
            }
            printf("\n");
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值