ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

所有硬币组合问题——动态规划hdu2069

2019-08-18 10:56:54  阅读:373  来源: 互联网

标签:hdu2069 硬币 int money coins cent 动态 dp coin


Problem Description Suppose there are 5 types of coins: 50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make changes with these coins for a given amount of money.

For example, if we have 11 cents, then we can make changes with one 10-cent coin and one 1-cent coin, or two 5-cent coins and one 1-cent coin, or one 5-cent coin and six 1-cent coins, or eleven 1-cent coins. So there are four ways of making changes for 11 cents with the above coins. Note that we count that there is one way of making change for zero cent.

Write a program to find the total number of different ways of making changes for any amount of money in cents. Your program should be able to handle up to 100 coins.  

 

Input The input file contains any number of lines, each one consisting of a number ( ≤250 ) for the amount of money in cents.  

 

Output For each input line, output a line containing the number of different ways of making changes with the above 5 types of coins.  

 

Sample Input 11 26   现附上AC代码:

#include<iostream>
using namespace std;
const int money=251;
const int coin=101;
int dp[money][coin]={0}; //dp[i][j]表示金额为i,硬币数为j的种类方法
int value[5]={1,5,10,25,50};

void solve()
{
//for(int i=0;i<money;i++) 这里为什么只能dp[0][0]=0,是因为dp[j][1]=dp[j][1]+dp[j-value[i]][k-1],只有j-value[i]==0时,才能在加1
//dp[i][0]=1;
dp[0][0]=1;
for(int i=0;i<5;i++)
{
for(int j=value[i];j<money;j++)
{
for(int k=1;k<coin;k++)
{
dp[j][k]=dp[j][k]+dp[j-value[i]][k-1];
}
}
}
}
int main()
{
solve();
int ans[money]={0};
ans[0]=1; //注意这一步,一定不能忘,一个特殊值
for(int i=1;i<money;i++)
{
for(int j=1;j<coin;j++)
{
ans[i]=dp[i][j]+ans[i];
}
}
int s;
while(cin>>s)
cout<<ans[s]<<endl;
return 0;
}

 

因为刚开始学习动态规划,这种硬币问题算是入门的问题。到现在为止,我所理解的动态规划是利用递推关系式,对于某个解值是需要用之前的算出来的进行求解,也就是类似于我要想知道第三个值的数,就需要让第一个数乘以第二个数,举个例子,就像我们都熟知的斐波那契数列,但要注意初始值一定要弄明白,否则递推关系式将进行不下去。另外斐波那契数列是最简单的dp,稍微复杂一点的需要进行不止一次地递推,就像这次的硬币问题,需要递推5次。

标签:hdu2069,硬币,int,money,coins,cent,动态,dp,coin
来源: https://www.cnblogs.com/sunjianzhao/p/11371647.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有