qdu-凑数题(01背包)
2024-08-29 21:00:05
Description
小Q手里有n(n<=1000) 个硬币,每枚硬币有一定的金额(200=>x>=1)他想知道,用这些硬币(每枚硬币只能用一次,但可能会有等面值的用两次) 能组成多少种不同的金额?
Input
第一行 n,表示第二行一共有n个数字,第二行 表示n个数字
Output
第一行 输出 m, 表示可以组成多少种不同的金额第二行 按照从小到大的顺序输出所有的金额。 注意,每行的结尾,不要有空格,否则你的答案可能会被判错。
Sample Input 1
2
1 2
Sample Output 1
3
1 2 3
Sample Input 2
2
1 1
Sample Output 2
2
1 2 01背包的未装满的情况
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<cmath>
#define Inf 0x3f3f3f3f
const int maxn=1e5+;
typedef long long ll;
using namespace std;
int a[maxn],dp[];
int main()
{
int n;
cin>>n;
int sum=;
for(int t=;t<=n;t++)
{
scanf("%d",&a[t]);
sum+=a[t];
}
for(int t=;t<=sum;t++)
{
dp[t]=-Inf;
}
dp[]=;
for(int t=;t<=n;t++)
{
for(int j=sum;j>=a[t];j--)
{
dp[j]=max(dp[j],dp[j-a[t]]+a[t]);
}
}
int s[];
int cnt=;
for(int t=;t<=sum;t++)
{
if(dp[t]>=)
{ s[cnt++]=dp[t];
}
}
cout<<cnt<<endl;
for(int t=;t<cnt;t++)
{
if(t==)
{
printf("%d",s[t]);
}
else
{
printf(" %d",s[t]);
}
}
return ;
}
最新文章
- ecshop随机分类
- ReSharper 配置及用法
- Linq 查询结果 可能遵循 2 &#186;,2&#185;,2 &#178;,......增长计算
- 总结CSS3新特性(Transform篇)
- CSS控制XML与通过js解析xml然后通过html显示xml中的数据
- Two-Phase Commit (两阶段提交)
- matlab参数查询
- flash视频器播放器代码
- C# 将\u1234类型的字符转化成汉字
- DDD实践案例:引入事件驱动与中间件机制来实现后台管理功能
- Java Primitives and Bits
- freemarker.core.ParseException:Unexpected end of file reached
- centos7 编译安装greenplum5.7
- iOS 8 中如何集成 Touch ID 功能
- Ajax技术使用补充
- CAP:Alantany 谈 CAP
- php 页面调转导致session丢失解决方法
- nodejs多语句查询
- Python conda 入门
- 小程序登录、微信网页授权(Java版)