题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2069

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

Sample Output

4
13

解题思路:这道题可以用暴力枚举直接解决。枚举每种硬币的数量为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)的更多相关文章

  1. HDU 2069 Coin Change

    Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...

  2. hdu 2069 Coin Change(完全背包)

    Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  3. HDU 2069 Coin Change(完全背包变种)

    题意:给你5种银币,50 25 10 5 1,问你可以拼成x的所有可能情况个数,注意总个数不超过100个 组合数问题,一看就是完全背包问题,关键就是总数不超过100个.所有我们开二维dp[k][j], ...

  4. HDOJ 2069 Coin Change(母函数)

    Coin Change Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. hdu2069(Coin Change)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2069 Coin Change Time Limit: 1000/1000 MS (Java/Other ...

  6. 题解报告:hdu 1398 Square Coins(母函数或dp)

    Problem Description People in Silverland use square coins. Not only they have square shapes but also ...

  7. 题解报告: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 ...

  8. 【LeetCode】518. Coin Change 2 解题报告(Python)

    [LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...

  9. JSU省赛队员选拔赛个人赛1(Coin Change、Fibbonacci Number、Max Num、单词数、无限的路、叠筐)

    JSU省赛队员选拔赛个人赛1 一.题目概述: A.Coin Change(暴力求解.动态规划)     B.Fibbonacci Number(递推求解) C.Max Num(排序.比较) D.单词数 ...

随机推荐

  1. 使用CEF类库处理HTTP请求

    当我们基于CEF开发应用时,可能会有URL请求处理的需求,比如HTTP下载或上传,此时可以利用CEF提供的类库来完成,而不必自己实现或引入其它第三方的类库. 在CEF里为URL Request设计了两 ...

  2. Linux系统启动流程分析

    作者:郭孝星 微博:郭孝星的新浪微博 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells Github:https://github.co ...

  3. C++开发人脸性别识别教程(8)——搭建MFC框架之读取目录信息

    在上一篇博客中我们已经绘制了MFC界面,在这篇博客中我们将加入响应代码,为MFC框架加入一个最主要的功能:打开一个目录. 一.加入相关头文件 这里头文件主要包括三类:opencv头文件.批量读取文件相 ...

  4. cocos2dx 制作单机麻将(五)

    cocos2dx 制作单机麻将(五) 麻将逻辑6 最基础的4人麻将逻辑(轮流循环出牌, 之前学的都能用上  跑起来了!!!) 最基础的麻将逻辑 依据自己须要 设置麻将人数GAME_PLAYER 基本流 ...

  5. 避免使用vector&lt;bool&gt;

     作为一个STL容器,vector<bool>仅仅有两点不正确. 首先.它不是一个STL容器. 其次,它并不存储bool.除此之外.一切正常. 一个对象要成为容器,就必须满足C++标准 ...

  6. Centos java 安装

    第一步:查看Linux自带的JDK是否已安装 (卸载centOS已安装的1.4) 安装好的CentOS会自带OpenJdk,用命令 java -version ,会有下面的信息: java versi ...

  7. OpenCV基本图像容器Mat的几种创建方法

    參考文章:http://www.cnblogs.com/tornadomeet/archive/2012/07/19/2599376.html 实验说明: (引用) 本文主要讲一些opencv 2.0 ...

  8. java类载入器——ClassLoader

    Java的设计初衷是主要面向嵌入式领域,对于自己定义的一些类,考虑使用依需求载入原则.即在程序使用到时才载入类,节省内存消耗,这时就可以通过类载入器来动态载入. 假设你平时仅仅是做web开发,那应该非 ...

  9. CronTab命令实例

    每2分钟 将date写入到time.log(以下的为奇数分钟运行) */2 * * * * date >> ~/time.log 1-59/2 * * * * date >> ...

  10. [IT学习]转载python 项目 计算器

    这个是从网上搜到的Python小项目之计算器(原文地址:http://www.2cto.com/kf/201402/279637.html).但该段代码估计是Python 2 写的. 如果你使用的程序 ...