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. org.springframework.beans.BeanUtils的用法

    s,t为对象BeanUtils.copyProperties(s, t);  将给定源bean的属性值复制到目标bean中 public static void copyProperties(Obje ...

  2. 【洛谷p1190】接水问题

    接水问题[题目链接] 咱也不知道为啥咱就是想写博客emmm 尽管这个题是2010年的普及组但是我太菜了并不会写emm 其实感觉这道题是纯纯正正的大模拟: 算法标签中的贪心是没有意义的啊?? SOLUT ...

  3. python-docx 添加表格时很慢的解决方法

    我们做监控系统的时候常需要给客户发送邮箱报告,附带一个word的文档,文档中插入表格给用户更直观的数据. 我用的时python-docx库操作文档,最近碰到,当往文档中插入表格时,随着表格行数的增多, ...

  4. PCIe基础篇(一)、基础知识扫盲

    1.PCIe:Peripheral Component interconnect Expess,外围组件接口互联,属于第三代IO总线,PCIe的传输速率指的是实际的有效传输速率,为RAW data(原 ...

  5. Canvas和SVG的比较

    Canvas 和 SVG 都允许您在浏览器中创建图形,但是它们在根本上是不同的. SVG SVG 是一种使用 XML 描述 2D 图形的语言. SVG 基于 XML,这意味着 SVG DOM 中的每个 ...

  6. SQLyog安装

    安装 使用 首先看到下面的界面

  7. runtime 理解笔记

    runtime 简称运行时,是系统运行的一种机制,在oc中通过c语言编写一个运行系统库.考进行一些非常底层的操作(oc无法完成的). 1.利用runtime,在程序运行过程中,动态创建一个类(比如KV ...

  8. 《图解HTTP》阅读总结

    前言:<图解HTTP>是一本图文并茂的好书,讲解地很详尽.目前我只完整地读了一遍,很多地方知其然不知其所以然,接下来打算抽空再读一次.这篇博文只是做个粗略记录,第二遍读完会再来编辑细化. ...

  9. postmaster - PostgreSQL多用户数据库服务器

    SYNOPSIS postmaster [ -A 0 | 1] [ -B nbuffers] [ -c name=value] [ -d debug-level] [ -D datadir] [ -F ...

  10. node npm vue.js 笔记

    cnpm 下载包的速度更快一些. 地址:http://npm.taobao.org/ 安装cnpm: npm install -g cnpm --registry=https://registry.n ...