hdu1709 The Balance

来源:互联网 发布:centos6网络配置 编辑:程序博客网 时间:2024/05/14 16:32

The Balance

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4597    Accepted Submission(s): 1842


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
31 2 439 2 1
 

Sample Output
024 5
 

Source
HDU 2007-Spring Programming Contest
 

Recommend
lcy
 

Statistic | Submit | Discuss | Note
题意是给你几个砝码让你求出它们不能组成的重量(砝码可以放在一个盘也可以放在不同盘),先输出个数再输出不能得到的重量
在百度上看的这题要用母函数,看了一下母函数是什么,有看了一下用法,然后自己慢慢写出来了,说实话还是有点成就感的,虽然今天队友问我我都有点忘记这是我做的了,哈哈。。。。
#include<stdio.h>#include<math.h>int c2[100000],c1[100000];int main(){int i,j,k,s,m,x,n,a[105];while(scanf("%d",&n)!=EOF){for(i=0,s=0;i<n;i++){scanf("%d",&a[i]);s+=a[i];//s用来求和,也就是最多可能出现的所有重量个数}for(i=0;i<s;i++){c2[i]=0;//标记的,看i是否已出现}for(i=0,k=0,m=0;i<n;++i){if(c2[a[i]-1]==0)//如果这个砝码本身还没有被加进这个数组,那就把它加进去{c1[m]=a[i];c2[a[i]-1]++;m++;}for(j=0;j<k;++j)//小于k是为了避免一个砝码被重复用来组合{x=a[i]+c1[j];//与前面所有的已有数据作和if(c2[x-1]==0)//如果新产生的数在以前没出现{c1[m]=x;//假如数组c2[x-1]++;m++;}x=abs(a[i]-c1[j]);//与前面所有数据做差,因为可能与其他砝码分放在两边if(x>0&&c2[x-1]==0)//这就和上面的作用一样,x>0是为了防止数组越界
{c1[m]=x;c2[x-1]++;m++;}}k=m;//m是加入当前玛法后产生不同重量的总数}printf("%d\n",s-k);//k是最终所有可能出现的重量,s是总重量也是最多可能出现的个数if(k!=s){for(j=0,i=0;i<s;i++){if(c2[i]==0)if(j!=0)//这一步和下面的主要是格式控制printf(" %d",i+1);else {j++;printf("%d",i+1);}}printf("\n");}}return 0;}


原创粉丝点击