博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 2069Coin Change(dp)
阅读量:4049 次
发布时间:2019-05-25

本文共 1909 字,大约阅读时间需要 6 分钟。

Coin Change

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17261    Accepted Submission(s): 5905


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
1126
 

Sample Output
413
 

Author
Lily
 

Source
 

Recommend
linle
他要求硬币总数不超过100
 这时候我们就要增加一个维度来限制数量,dp[i][j]表示用不超过i个硬币组成j的总数
最外一层枚举硬币总类和上面一样的道理,dp[num][j] += dp[num - 1][j - c[i]]这里是对于新来的每一个硬币,= 放这个硬币的总数+不放这个硬币的总数,
d[num-1][j-c[i]]是之前已经更新到有k-1个新硬币的了,而这个式子只针对地k个新硬币所以num-1是未放这个新硬币之前的答案,即dp[j - c[i]]放的都只是放一个硬币
(第k个新硬币)得到的总数
 
然后答案要累加不超过i:0-->100得到的总数,一定要从0开始累加,因为数据有0个硬币的总数
#include
#include
#include
using namespace std;int dp[500][500];int main() { int n,m,i,k,j,h,sum; int c[]={0,1,5,10,25,50}; while(cin>>h) { memset(dp,0,sizeof(dp)); dp[0][0]=1; for(i=1;i<=5;i++) { for(k=1;k<=100;k++)//因为不能超过100枚 所以需要二维数组 { for(j=c[i];j<=h;j++) { dp[k][j]+=dp[k-1][j-c[i]]; } } } sum=0; for(i=0;i<=100;i++) { sum+=dp[i][h]; } cout<
<

转载地址:http://wxfci.baihongyu.com/

你可能感兴趣的文章
剑指Offer 13.机器人的运动范围——DFS和BFS
查看>>
Java中GUI编程总结—AWT中的Frame容器、panel面板、布局管理
查看>>
剑指offer12.矩阵中的路径—DFS+剪枝
查看>>
Java中GUI编程总结—事件监听、TextField监听、画笔、(鼠标、窗口、键盘)监听
查看>>
Java中GUI编程总结—Swing(窗口、面板、弹窗、标签、按钮、列表、文本框)
查看>>
Java中map容器分别根据键key和值value进行排序的总结
查看>>
剑指offer面试题16. 数值的整数次方——快速幂
查看>>
剑指 Offer 39. 数组中出现次数超过一半的数字——摩尔投票法
查看>>
python中SQLite3 数据库语句使用总结——增删改查
查看>>
Java网络编程总结
查看>>
leetcode 477. 汉明距离总和——超出时间限制
查看>>
基于SSM校园二手交易市场系统——课程设计(毕业设计)
查看>>
leetcode 1882.使用服务器处理任务——优先队列
查看>>
leetcode 523.连续的子数组的和——前缀和+哈希表
查看>>
Java中的set的toArray()转成的数组如何进行接收
查看>>
剑指offer 43 1~n整数中1出现的次数
查看>>
基于SSM的图书馆管理系统——计算机类专业课程设计(毕业设计)
查看>>
leetcode 1239. 串联字符串的最大长度——回溯+位运算
查看>>
基于SSH在线考试系统(计算机专业认证考试)——计算机类专业课程设计(毕业设计)
查看>>
Springboot的仓库管理系统——计算机类专业课程设计(毕业设计)
查看>>