[HDOJ]Coin Change(DP)
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=2069
题意
有面值1,5,10,25,50的硬币数枚,对于输入的面值n,输出可凑成面值n(且限制总硬笔数小于等于100枚)的方案数。特别的,n=0时方案数=1。
其中,输入n<=250。
思路
DP。
状态 ways[j][i] 表示面值等于j且硬币枚数等于i时的方案数。
初始化时,只需将ways[0][0]=1即可,其他为0;
外层先遍历硬币面值种类,这层遍历的具体顺序不重要,即保证有不重复累加同样的组合即可;
中间层遍历硬币枚数,对从小到大硬币枚数,显然需正序;再遍历各种面值,这要正序,原理同完全背包;状态转移方程见代码。
最终输出答案时做相应的简单求和即可。
代码
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <set>
using namespace std;
#define MAX_MONEY 250
#define COIN_CNT 100
long long ways[MAX_MONEY+5][COIN_CNT+5];
int main(int argc, const char * argv[]) {
set<int> coin={1,5,10,25,50};
memset(ways, 0,sizeof(ways));
ways[0][0]=1;
for(auto it=coin.begin();it!=coin.end();++it){
for(int i=1;i<=COIN_CNT;++i){
for(int j=*it;j<=MAX_MONEY;++j){
ways[j][i]+=ways[j-*it][i-1];
}
}
}
int money;
while(~scanf("%d",&money)){
long long ans=0;
for(int i=0;i<=COIN_CNT;i++){
ans+=ways[money][i];
}
printf("%lld\n",ans);
}
return 0;
}
[HDOJ]Coin Change(DP)的更多相关文章
- UVA.674 Coin Change (DP 完全背包)
UVA.674 Coin Change (DP) 题意分析 有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 每种硬币的数量是无限的.典型完全背包. 状态 ...
- LeetCode OJ 322. Coin Change DP求解
题目链接:https://leetcode.com/problems/coin-change/ 322. Coin Change My Submissions Question Total Accep ...
- UVA 674 Coin Change(dp)
UVA 674 Coin Change 解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/ ...
- HDOJ 2069 Coin Change(母函数)
Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- UVA 674 Coin Change 换硬币 经典dp入门题
题意:有1,5,10,25,50五种硬币,给出一个数字,问又几种凑钱的方式能凑出这个数. 经典的dp题...可以递推也可以记忆化搜索... 我个人比较喜欢记忆化搜索,递推不是很熟练. 记忆化搜索:很白 ...
- [LeetCode] Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- Epic - Coin Change
Something cost $10.25 and the customer pays with a $20 bill, the program will print out the most eff ...
- C - Coin Change (III)(多重背包 二进制优化)
C - Coin Change (III) Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- Coin Change (II)(完全背包)
Coin Change (II) Time Limit: 1000MS Mem ...
随机推荐
- c# sql等微型代码工具LinqPad
- java 浅克隆(浅复制)和深克隆(深复制)
http://www.voidcn.com/blog/u011380813/article/p-6161450.html https://gold.xitu.io/entry/570d89651ea4 ...
- linux文本处理笔记
cut: 按列操作文本 sort: 排序 uniq: 去重,去除连续重复行 cut -d 'delimiter' -f start-end filename.txt # -d 表示分割符号,del ...
- Linux命令:findutils
本篇介绍Linux中常用的文件查找和定位工具,包括:find.locate.which.xargs等. GNU find 命令参考<https://www.gnu.org/software/fi ...
- Linux下tomcat运行命令
tomcat启动 [root@master webapps]# /usr/local/tomcat7.0/bin/catalina.sh start startup.sh的源代码,其实就是执行 c ...
- 创建一个Maven Web应用程序
1 在Eclipes创建maven,首先File new ,在other 中找到Maven,Maven Project,下一步选择WebApp,创建Maven工程名字,完成 2 在新建的Maven工程 ...
- [jQ]jQuery显式操作Checkbox,并用数组存储关联值的方案
---------------------------------------------------------------------------------------------------- ...
- thread == 售票
import org.apache.xerces.util.SymbolTable; public class ThreadDemo1 { public static void main(String ...
- win 2012 安装mysql 5.7.20 及报错 This application requires Visual Studio 2013 Redistributable. Please install the Redistributable then run this installer again 的解决办法
本文地址:http://www.cnblogs.com/jying/p/7764147.html 转载请注明出处. 安装过程其实挺简单,基本上下一步下一步,可以参考我的另一篇mysql安装文章: ...
- cv2对图像进行旋转和放缩变换
旋转: def get_image_rotation(image): #通用写法,即使传入的是三通道图片依然不会出错 height, width = image.shape[:2] center = ...