题解报告:hdu 2069 Coin Change(暴力orDP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2069
Problem Description
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
Output
Sample Input
Sample Output
解题思路:这道题可以用暴力枚举直接解决。枚举每种硬币的数量为0~n/这种币值即可,为了避免TLE超时,最后一种硬币换成表达式来判断,用num来计数情况,其中注意所有币值的总数量<=100。
AC代码一(直接暴力):
- #include<bits/stdc++.h>
- using namespace std;
- int main()
- {
- int n,num;
- while(cin>>n){
- num=;
- for(int a=;a*<=n;a++){
- for(int b=;b*<=n;b++){
- for(int c=;c*<=n;c++){
- for(int d=;d*<=n;d++){//剩下一步由减法来,避免超时
- if(n-a*-b*-c*-d*>= && a+b+c+d+n-a*-b*-c*-d*<=)num++;
- }
- }
- }
- }
- cout<<num<<endl;
- }
- return ;
- }
AC代码二之dp:先贴一下此题的思路:题解报告:hdu 1284 钱币兑换问题(简单数学orDP)这题就是多加了一个维度,因为题目中规定了硬币的数量最多取100个,因此定义dp[k][j]表示前k个硬币组成钱j的总方案数,那么易得状态转移方程:dp[k][j]+=dp[k-1][j-a[i]],意思是减去当前某种一个币值,那么就会增加前k-1个硬币组成钱j-a[i]的方案数dp[k-1][j-a[i]](对于同一种硬币a[i]来讲)。预处理打表,然后累加用k(k∈[0,100])个硬币组成钱n的所有方案数即为最终的方案总数。注意:①初始化dp[0][0]=1,表示前0个硬币组成钱0的方案数为1(原因和上面链接博文里的一样)答案要累加不超过i:0-->100得到的总方案数,一定要从0开始累加,因为有0个硬币时的方案数。
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int n,sum,a[]={,,,,},dp[][];
- memset(dp,,sizeof(dp));dp[][]=;
- for(int i=;i<;++i)//种数
- for(int k=;k<=;++k)//硬币总数不超过100
- for(int j=a[i];j<=;++j)
- dp[k][j]+=dp[k-][j-a[i]];
- while(cin>>n){
- sum=;
- for(int k=;k<=;++k)sum+=dp[k][n];//累加用0~100组成钱n的所有方案数
- cout<<sum<<endl;
- }
- return ;
- }
题解报告:hdu 2069 Coin Change(暴力orDP)的更多相关文章
- HDU 2069 Coin Change
Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- hdu 2069 Coin Change(完全背包)
Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 2069 Coin Change(完全背包变种)
题意:给你5种银币,50 25 10 5 1,问你可以拼成x的所有可能情况个数,注意总个数不超过100个 组合数问题,一看就是完全背包问题,关键就是总数不超过100个.所有我们开二维dp[k][j], ...
- HDOJ 2069 Coin Change(母函数)
Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu2069(Coin Change)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2069 Coin Change Time Limit: 1000/1000 MS (Java/Other ...
- 题解报告:hdu 1398 Square Coins(母函数或dp)
Problem Description People in Silverland use square coins. Not only they have square shapes but also ...
- 题解报告:hdu 1028 Ignatius and the Princess III(母函数or计数DP)
Problem Description "Well, it seems the first problem is too easy. I will let you know how fool ...
- 【LeetCode】518. Coin Change 2 解题报告(Python)
[LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...
- JSU省赛队员选拔赛个人赛1(Coin Change、Fibbonacci Number、Max Num、单词数、无限的路、叠筐)
JSU省赛队员选拔赛个人赛1 一.题目概述: A.Coin Change(暴力求解.动态规划) B.Fibbonacci Number(递推求解) C.Max Num(排序.比较) D.单词数 ...
随机推荐
- 再谈用java实现Smtp发送邮件之Socket编程
很多其它内容欢迎訪问个人站点 http://icodeyou.com 前几天利用Socket实现了用java语言搭建webserver,全程下来应该会对Socket这个东西已经使用的很熟悉了.尽管 ...
- 【LeetCode】Swap Nodes in Pairs 链表指针的应用
题目:swap nodes in pairs <span style="font-size:18px;">/** * LeetCode Swap Nodes in Pa ...
- python第四讲
三元运算符: 三元运算又叫三目运算,是对简单的条件语句的缩写. 书写格式: n1 = 值1 if 条件 else 值2 # 如果条件成立,那么将 “值1” 赋值给n1变量,否则,将“值2”赋值给n1变 ...
- javascript 语法规范错误提示代码
“Missing semicolon.” : “缺少分号.”, “Use the function form of \”use strict\”.” : “使用标准化定义function.”, “Un ...
- HDU 1824 Let's go home (2-SAT判定)
Let's go home Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- C语言将10进制转为2进制
第一种方法: #include<stdio.h> void dectobin(int n); int main() { int x=0; scanf("%d",& ...
- Bean Query 第一个版本号(1.0.0)已公布
BeanQuery 是一个把对象转换为Map的Java工具库. 支持选择Bean中的一些属性.对结果进行排序和依照条件查询. 不只能够作用于顶层对象,也能够作用于子对象.很多其它具体的介绍能够看我的博 ...
- YTU 2437: C++ 习题 比较大小-类模板
2437: C++ 习题 比较大小-类模板 时间限制: 1 Sec 内存限制: 128 MB 提交: 1144 解决: 805 题目描述 声明一个类模板,利用它分别实现两个整数.浮点数和字符的比较 ...
- Error: Target id 'android-5' is not valid. Use 'android list targets' to get the target ids.
输入命令: lianxumacdeMac-mini-2:hello-jni lianxumac$ android list targets Available Android targets: --- ...
- Python进程间通信Queue
1.Queue使用方法: Queue.qsize():返回当前队列包含的消息数量: Queue.empty():如果队列为空,返回True,反之False : Queue.full():如果队列满了, ...