UVA--674 Coin Change(完全背包)
2013-12-23 01:39 gongti 阅读(209) 评论(0) 编辑 收藏 举报题目http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=615
分析:计算背包方案数目
#include<cstdio>
#include<cstring>
using namespace std;
int a[5]={1,5,10,25,50},dp[7500];
int amount;
int main()
{
while (scanf("%d",&amount)!=EOF)
{
memset(dp,0,sizeof(dp));
dp[0]=1;
for(int i=0;i<5;i++)
for(int j=a[i];j<=amount;j++)
dp[j]=dp[j]+dp[j-a[i]];
printf("%d\n",dp[amount]);
}
return 0;
}
#include<cstring>
using namespace std;
int a[5]={1,5,10,25,50},dp[7500];
int amount;
int main()
{
while (scanf("%d",&amount)!=EOF)
{
memset(dp,0,sizeof(dp));
dp[0]=1;
for(int i=0;i<5;i++)
for(int j=a[i];j<=amount;j++)
dp[j]=dp[j]+dp[j-a[i]];
printf("%d\n",dp[amount]);
}
return 0;
}