ZCMU-1313-砝码称重
2017-01-12 12:02
288 查看
1313: The Balance
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 32 Solved: 14
[Submit][Status][Web
Board]
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 qualityof 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 qualityof 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 4
3
9 2 1
Sample Output
02
4 5
HINT
For the second case,1=1,2=2,3=1+2,6=9-2-1,7=9-2,8=9-1,9=9,10=9+1,11=9+2,12=9+1+2,but,4,5 can not reach.【解析】
这道题的意思就是给你n个砝码和n个砝码的重量。问你在这些砝码的总和当中那个质量是称不出来的。在这里我们又用到了母函数...模板是一样的就是拿来把它套出来。注意的是这里可以用砝码相减了。所以我们在代码中也要体现这一点,之前有个给你砝码的数量和质量让你求能称出什么来的。#include<cstdio>
#include<cstring>
#include<algorithm&g
cf39
t;
#include<cmath>
#include<map>
#include<string>
using namespace std;
int a[101],b[101];
int f1[11000],f2[11000];
int main()
{
int n,i,sum,m,j,k;
while(~scanf("%d",&n))
{
int cnt=0,sum1=0;
memset(f1,0,sizeof(f1));
memset(f2,0,sizeof(f2));
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum1+=a[i];//所以砝码质量的总和
}
f1[0]=f1[a[0]]=1;//此处f1[0]一定要注意为1,因为它本身的砝码肯定是能称出来的
sum=a[0];//当前砝码的质量总和
for(i=1;i<n;i++)
{
for(j=0;j<=sum;j++)//从0开始到当前加上的砝码的质量
{
for(k=0;k+j<=sum1&&k<=a[i];k+=a[i])//k+j最大肯定不可能比所有砝码质量加起来那么大。
{
m=abs(j-k);//由于砝码的质量也可以通过相减称出来,比如6可以是9克的砝码减去2克的和1克的
f2[j+k]+=f1[j];//模拟多项式相乘,下标表示指数相乘,
f2[m]+=f1[j];
}
}
sum=sum+a[i];
for(j=0;j<=sum;j++)
{
f1[j]=f2[j];//多项式的结果给了f1数组
f2[j]=0;
}
}
int p=0,flag=0;
for(i=1;i<=sum;i++)
{
if(f1[i]==0)
{
cnt++;
b[p++]=i;
flag=1;
}
}
printf("%d\n",cnt);
if(flag)
{
printf("%d",b[0]);
for(i=1;i<p;i++)
printf(" %d",b[i]);
printf("\n");
}
}
return 0;
}
相关文章推荐
- 还在为AndroidStudio的Gradle版本配置头疼?看看老司机的解决方法吧
- 一个JSP分页代码
- JZ2440串口打印字符作为调试
- 数据库的各种公共方法集合
- Swift-常用控件创建(UIAlertController,UITableView)等等
- String与StringBuffer之间的区别
- xml操作(二)添加xml节点的子节点
- 1021 石子归并(区间DP)
- 数据分析入门必看:3个选择方向及技能要求!
- 多线程高并发学习笔记(1)http://www.cnblogs.com/samqiu/p/4279836.html
- 棋局评估
- [Android 知识点] 自定义View(三)
- 使用 mysqldump 迁移 MySQL 数据
- Swift3-基础语法
- rxjs5.X系列 —— ErrorHandling/Condition/Mathematical系列 api 笔记
- iOS企业开发plist安装包实现
- 适合于图像处理方向的SCI期刊杂志列表
- JBOSS START/STOP/RESTART shell script
- 移动端点击态处理的三种实现方式
- 重建二叉树