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的更多相关文章

  1. CJOJ 1071 【Uva】硬币问题(动态规划)

    CJOJ 1071 [Uva]硬币问题(动态规划) Description 有n种硬币,面值分别为v1, v2, ..., vn,每种都有无限多.给定非负整数S,可以选用多少个硬币,使得面值之和恰好为 ...

  2. 【BZOJ1042】硬币购物(动态规划,容斥原理)

    [BZOJ1042]硬币购物(动态规划,容斥原理) 题面 BZOJ Description 硬币购物一共有4种硬币.面值分别为c1,c2,c3,c4.某人去商店买东西,去了tot次.每次带di枚ci硬 ...

  3. 腾讯笔试题:小Q硬币组合

    腾讯有一道机试题: 大概意思是: 小Q非常富有,拥有非常多的硬币,小Q的拥有的硬币是有规律的,对于所有的非负整数K,小Q恰好> 各有两个数值为2^k,的硬币,所以小Q拥有的硬币是1,1,2,2, ...

  4. Bzoj 1042: [HAOI2008]硬币购物 容斥原理,动态规划,背包dp

    1042: [HAOI2008]硬币购物 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1747  Solved: 1015[Submit][Stat ...

  5. 记录结果再利用的"动态规划"之背包问题

    参考<挑战程序设计竞赛>p51 https://www.cnblogs.com/Ymir-TaoMee/p/9419377.html 01背包问题 问题描述:有n个重量和价值分别为wi.v ...

  6. [Leetcode][动态规划] 零钱兑换

    一.题目描述 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: ...

  7. [LeetCode] 518. Coin Change 2 硬币找零 2

    You are given coins of different denominations and a total amount of money. Write a function to comp ...

  8. Leetcode题目322.零钱兑换(动态规划-中等)

    题目描述: 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输入: c ...

  9. eetcode必要技巧--动态规划(一)

    首先我们要搞清楚什么是动态规划 动态规划是运筹学中用于求解决策过程中的最优化数学方法.当然,我们在这里关注的是作为一种算法设计技术,作为一种使用多阶段决策过程最优的通用方法. 当然这个很难理解,但是按 ...

随机推荐

  1. HTTP协议详解??

    HTTP协议: HTTP (hypertext transport protocol) , 即 超 文 本 传 输 协 议 . 这 个 协 议 详 细 规 定 了 浏 览 器 和 万 维 网 服 务 ...

  2. C# asp.net XML格式的字符串显示不全

    前台显示XML字符串显示不全 后台XML字符串使用<xmp></xmp>将XML格式字符串括起来

  3. day01-html

    HTML概述: HTML: Hyper Text Markup Language 超文本标记语言 超文本: 比普通文本功能更加强大,可以添加各种样式 标记语言: 通过一组标签.来对内容进行描述. &l ...

  4. linux创建相关待同步目录

    [root@rsync-server-1 ~]# mkdir /data/{web,web_data}/redhat.sx -p [root@rsync-server-1 ~]# tree /data ...

  5. VS2012在解决方案资源管理器显示解决方案名称

    工具>选项>项目与解决方案(需点右下方的显示所有)>点上 总是显示解决方案

  6. C6678芯片

    TMS320C6678是一款八核C66x的定点/浮点DSP,支持高性能信号处理应用.TMS320C6678芯片是美国德州仪器公司生产的处理器.它支持高性能信号处理应用,支持DMA传输,可应用于高端图像 ...

  7. Solr从数据库导入数据(DIH)

    一. 数据导入(DataImportHandler-DIH) DIH 是solr 提供的一种针对数据库.xml/HTTP.富文本对象导入到solr 索引库的工具包.这里只针对数据库做介绍. A.准备以 ...

  8. Oracle Internals Notes Redo Write Triggers

    There are four conditions that cause LGWR to perform a redo write. When LGWR is idle, it sleeps on a ...

  9. tensorboard可视化(先写一点点)

    在tensorboard上显示运行图: import tensorflow as tf a = tf.constant(10,name="a") b = tf.constant(9 ...

  10. /usr,/usr/local/ 还是 /opt 目录区别

    Linux 的软件安装目录是也是有讲究的,理解这一点,在对系统管理是有益的 /us(Unix Software Resource)r:系统级的目录,可以理解为C:/Windows/, /usr/lib ...