HDU 2069 & UVA 674 Coin Change(换硬币 dp 入门经典水题,背包问题)
2016-05-06 20:35
579 查看
Coin Change
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
Sample Output
题意;
一共有 n 元钱,现在有 1 、5、10、25、50 元的钱,问你有多少种方式组合成 n 元钱!
换硬币,动态规划入门经典题型,没有看过《背包九讲》的可以看看《背包九讲》,所以我在这里也就不多说了,状态转移方程 dp[j] = dp[j] + dp[ j - a[i] ];
附上《背包九讲》链接 :/article/7677705.html 点击打开链接
附上代码:
Time Limit: 1000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
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
Sample Output
4 13
题意;
一共有 n 元钱,现在有 1 、5、10、25、50 元的钱,问你有多少种方式组合成 n 元钱!
换硬币,动态规划入门经典题型,没有看过《背包九讲》的可以看看《背包九讲》,所以我在这里也就不多说了,状态转移方程 dp[j] = dp[j] + dp[ j - a[i] ];
附上《背包九讲》链接 :/article/7677705.html 点击打开链接
附上代码:
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <vector> #define INF 0x3f3f3f3f using namespace std; int main() { int n; while(cin >> n) { int a[5] = {1,5,10,25,50}; long long int dp[8000] = {0}; // 需要用到 long long 结果有可能超出 int 范围 dp[0] = 1; for(int i = 0;i < 5;i++) { for(int j = a[i];j <= n;j++) { dp[j] = dp[j] +dp[j - a[i]]; } } cout << dp << endl; } return 0; }
相关文章推荐
- 22.复杂链表的复制
- 结构体字节对齐
- Hadoop安装及开发
- mock私有变量的时候出现的问题
- POJ3177 Redundant Paths
- C经典 结构图的三种定义方式
- cocos-js,时间相关的处理
- Hadoop HDFS Tools
- 手工MAVEN建立WEBAPP项目并打包部署
- lightoj 1074 spfa判断负环
- 日记帐导入-来源为“采购”日记帐导入失败问题
- 文件传输基础—Java IO流 FileInputStream 的运用
- deep learning 练习1 线性回归练习
- 虚拟化_KVM
- 代理模式:控制对对象的访问
- 前端-js-this
- CentOS允许/禁止ping的方法
- IE7-span折行问题
- HDU 3639 Hawk-and-Chicken tarjan缩点+dfs
- Android去除题栏