HDOJ_1709 The Balance 解题报告

本文介绍了一种使用平衡和若干重量来测量药品剂量的算法。通过输入不同数量和质量的重量,该算法能够找出无法测量的质量范围,并列出所有可实现的质量组合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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
 
 
解题思路:用母函数去求解,只是这个增加了可能有减得情况,主要还是组合的一个思想。
代码如下:
Code:
  1. #include<iostream>  
  2. using namespace std;  
  3. const int Max1(10001);  
  4. const int Max2(101);  
  5. int num[Max1];  
  6. int temp[Max1];  
  7. int data[Max2];  
  8. int main()  
  9. {  
  10.     int n;  
  11.     while(cin>>n)  
  12.     {  
  13.         int sum=0;  
  14.         num[0]=1;  
  15.         for(int i=1;i<=n;i++)  
  16.         {  
  17.             cin>>data[i];  
  18.             sum+=data[i];  
  19.               
  20.         }  
  21.         for(i=0;i<=sum;++i)  
  22.         {  
  23.             num[i]=0;  
  24.             temp[i]=0;  
  25.         }  
  26.         num[0]=1;  
  27.       //用到两重循环,从来到尾的找即可  
  28.         for(i=1;i<=n;i++)  
  29.         {  
  30.             for(int j=0;j+data[i]<=sum;j++)           
  31.             {  
  32.                 if(num[j]==1)  
  33.                 {  
  34.                     temp[j]=1;  
  35.                     temp[j+data[i]]=1;  
  36.                     temp[abs(j-data[i])]=1;    
  37.                 }  
  38.             }  
  39.             for(j=0;j<=sum;j++)  
  40.             {  
  41.                 num[j]=temp[j];  
  42.                 temp[j]=0;  
  43.             }  
  44.         }  
  45.         int step=0;  
  46.         int j;  
  47.         for(i=1,j=0;i<=sum;i++)  
  48.         {  
  49.             if(num[i]==0)  
  50.             {  
  51.                 step++;  
  52.                 temp[j]=i;  
  53.                 j++;  
  54.             }  
  55.         }  
  56.         cout<<step<<endl;  
  57.         if(step!=0)  
  58.         {  
  59.             for(i=0;i<j;i++)  
  60.             {  
  61.                 if(i!=0)  
  62.                     cout<<' ';  
  63.                 cout<<temp[i];  
  64.             }  
  65.             cout<<endl;  
  66.         }  
  67.     }  
  68.     return 0;  
  69. }  
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值