Description
People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some coins.He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn't know the exact price of the watch.
You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.
You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony's coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins.
Input
The input contains several test cases. The first line of each test case contains two integers n(1<=n<=100),m(m<=100000).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (1<=Ai<=100000,1<=Ci<=1000). The last test case is followed by two zeros.
Output
For each test case output the answer on a single line.
Sample Input
3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0
Sample Output
8
4题目的大意
有n种硬币 ,求n种硬币加起来不能超过m的方案数
对于dp还是要多进行思考,这个是一个多重背包的问题,详细的解说在代码中
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int n,m;
int a[205],c[205],dp[100005],vis[100005];
//dp表示该币值的硬币数量有多少个
//vis表示该币值的硬币是否被用过
while (cin>>n>>m)
{
memset(dp,0,sizeof(dp));
memset(vis,0,sizeof(vis));
vis[0]=1;
int cnt=0;
if(n==0&&m==0) break;
for (int i=0;i<n;i++)
scanf("%d",&a[i]);
for (int i=0;i<n;i++)
{
scanf("%d",&c[i]);
}
for (int i=0;i<n;i++)
{
memset(dp,0,sizeof(dp));//很好理解啊
for (int j=a[i];j<=m;j++)
{
//dp向前滚动
if(!vis[j]&&vis[j-a[i]]&&dp[j-a[i]]<c[i])//0<=dp<=c[i] 0<dp-1<c[i]
{
cnt++;
vis[j]=1;
dp[j]=dp[j-a[i]]+1;
}
}
}
printf("%d\n",cnt);
}
return 0;
}
本文介绍了一道关于硬币组合的编程题目,旨在通过动态规划解决特定条件下硬币组合的数量问题。具体而言,给定不同面额的硬币及数量,求在不超过限定总额的情况下,所有可能的价格组合数目。
1609

被折叠的 条评论
为什么被折叠?



