POJ1742:Coins(多重背包)
2013-07-27 20:34
387 查看
Description
PeopleinSilverlandusecoins.TheyhavecoinsofvalueA1,A2,A3...AnSilverlanddollar.OnedayTonyopenedhismoney-boxandfoundthereweresomecoins.Hedecidedtobuyaverynicewatchinanearbyshop.Hewantedtopaytheexactprice(withoutchange)andheknownthepricewouldnotmorethanm.Buthedidn'tknowtheexactpriceofthewatch.
Youaretowriteaprogramwhichreadsn,m,A1,A2,A3...AnandC1,C2,C3...CncorrespondingtothenumberofTony'scoinsofvalueA1,A2,A3...Anthencalculatehowmanyprices(form1tom)Tonycanpayusethesecoins.
Input
Theinputcontainsseveraltestcases.Thefirstlineofeachtestcasecontainstwointegersn(1<=n<=100),m(m<=100000).Thesecondlinecontains2nintegers,denotingA1,A2,A3...An,C1,C2,C3...Cn(1<=Ai<=100000,1<=Ci<=1000).Thelasttestcaseisfollowedbytwozeros.
Output
Foreachtestcaseoutputtheansweronasingleline.
SampleInput
SampleOutput
#include<stdio.h>
#include<algorithm>
#include<string.h>
intdp[100005];
intsum[100005];
intv[105],c[105];
intmain()
{
inti,j,k,n,m;
while(~scanf("%d%d",&n,&m),n+m)
{
for(i=1;i<=n;i++)
scanf("%d",&v[i]);
for(i=1;i<=n;i++)
scanf("%d",&c[i]);
memset(dp,0,sizeof(dp));
dp[0]=1;
intans=0;
for(i=1;i<=n;i++)
{
memset(sum,0,sizeof(sum));
for(j=v[i];j<=m;j++)
{
if(!dp[j]&&dp[j-v[i]]&&sum[j-v[i]]<c[i])
{
dp[j]=1;
sum[j]=sum[j-v[i]]+1;
ans++;
}
}
}
printf("%d\n",ans);
}
return0;
}
PeopleinSilverlandusecoins.TheyhavecoinsofvalueA1,A2,A3...AnSilverlanddollar.OnedayTonyopenedhismoney-boxandfoundthereweresomecoins.Hedecidedtobuyaverynicewatchinanearbyshop.Hewantedtopaytheexactprice(withoutchange)andheknownthepricewouldnotmorethanm.Buthedidn'tknowtheexactpriceofthewatch.
Youaretowriteaprogramwhichreadsn,m,A1,A2,A3...AnandC1,C2,C3...CncorrespondingtothenumberofTony'scoinsofvalueA1,A2,A3...Anthencalculatehowmanyprices(form1tom)Tonycanpayusethesecoins.
Input
Theinputcontainsseveraltestcases.Thefirstlineofeachtestcasecontainstwointegersn(1<=n<=100),m(m<=100000).Thesecondlinecontains2nintegers,denotingA1,A2,A3...An,C1,C2,C3...Cn(1<=Ai<=100000,1<=Ci<=1000).Thelasttestcaseisfollowedbytwozeros.
Output
Foreachtestcaseoutputtheansweronasingleline.
SampleInput
310 124211 25 1421 00
SampleOutput
8 4
这道题与杭电的一道题的题意是相同的,之所以拿出来是因为杭电那道题的代码在这里过不了,所以贴一个新的代码
#include<algorithm>
#include<string.h>
intdp[100005];
intsum[100005];
intv[105],c[105];
intmain()
{
inti,j,k,n,m;
while(~scanf("%d%d",&n,&m),n+m)
{
for(i=1;i<=n;i++)
scanf("%d",&v[i]);
for(i=1;i<=n;i++)
scanf("%d",&c[i]);
memset(dp,0,sizeof(dp));
dp[0]=1;
intans=0;
for(i=1;i<=n;i++)
{
memset(sum,0,sizeof(sum));
for(j=v[i];j<=m;j++)
{
if(!dp[j]&&dp[j-v[i]]&&sum[j-v[i]]<c[i])
{
dp[j]=1;
sum[j]=sum[j-v[i]]+1;
ans++;
}
}
}
printf("%d\n",ans);
}
return0;
}
相关文章推荐
- POJ 1742 Coins 【多重背包的应用 + 理解】
- POJ 1742 Coins 混合三种背包问题
- poj1742- Coins(多重背包)
- poj 1742 Coins 多重背包变形
- POJ 1742 Coins (背包)
- poj 1742 Coins (多重背包)
- POJ-1742-多重背包-Coins
- poj 1742 Coins 多重背包变形
- poj 1742 coins 多重背包,只是用来标记可以组成多少个小于m的值
- POJ 1742 Coins 多重背包入门题
- HDU2844&&POJ1742-Coins(多重背包)
- POJ-1742:Coins(多重背包二进制求法)
- POJ 1742 Coins(多重背包变型,楼教主男人八题)
- poj 1742 Coins (背包)
- 挑战练习题2.3动态规划 poj1742 Coins 多重背包
- poj 1742coins(优化的多重背包)
- poj 1742 Coins (多重背包)
- POJ 1742 Coins( 单调队列优化多重背包)
- poj 1742 Coins(多重背包)
- POJ 1742 - Coins (dp 多重背包)