UVA 357 Let Me Count The Ways(全然背包)
UVA 357 Let Me Count The Ways(全然背包)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=293
题意:
有5种硬币: 1分 5分 10分 25分 和50分. 如今给你一个面值n, 问你有多少种方法能利用上述硬币组合出n分的金钱.
分析:
典型的全然背包问题.
本题的限制条件: 硬币钱数正好等于n
本题的目的条件: 求有多少种组合方法.
所以我们令dp[i][j]==x 表示用前i种硬币来构造j分金钱一共同拥有x种方法.
初始化: dp为全0. 但dp[0][0]=1.
状态转移: dp[i][j] = sum( dp[i-1][j] , dp[i][j-val[i]])
前者表示第i种硬币一个都不选, 后者表示至少选1个第i种硬币来用.
终于所求: dp[5][n].
程序实现用的滚动数组, 逆序递推. 所以dp仅仅有[j]这一维.
AC代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 30000+5; int n=5;
int val[]={1,5,10,25,50};
long long dp[maxn]; int main()
{
//初始化
memset(dp,0,sizeof(dp));
dp[0]=1; //递推
for(int i=0;i<n;i++)
{
for(int j=val[i];j<maxn;j++)
dp[j] += dp[j-val[i]];
} //处理每一个输入
int x;
while(scanf("%d",&x)==1)
{
if(dp[x]==1) printf("There is only 1 way to produce %d cents change.\n", x);
else printf("There are %lld ways to produce %d cents change.\n",dp[x],x);
} return 0;
}
UVA 357 Let Me Count The Ways(全然背包)的更多相关文章
- uva 357 Let Me Count The Ways(01背包)
题目连接:357 - Let Me Count The Ways 题目大意:有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 解题思路:和uva674是一 ...
- UVA.357 Let Me Count The Ways (DP 完全背包)
UVA.357 Let Me Count The Ways (DP 完全背包) 题意分析 与UVA.UVA.674 Coin Change是一模一样的题.需要注意的是,此题的数据量较大,dp数组需要使 ...
- UVa 357 - Let Me Count The Ways
题目大意:也是硬币兑换问题,与147.674用同样的方法即可解决. #include <cstdio> #include <cstring> #define MAXN 3000 ...
- UVA 10465 Homer Simpson(全然背包: 二维目标条件)
UVA 10465 Homer Simpson(全然背包: 二维目标条件) http://uva.onlinejudge.org/index.php? option=com_onlinejudge&a ...
- UVA 10306 e-Coins(全然背包: 二维限制条件)
UVA 10306 e-Coins(全然背包: 二维限制条件) option=com_onlinejudge&Itemid=8&page=show_problem&proble ...
- HDU 1248 寒冰王座(全然背包:入门题)
HDU 1248 寒冰王座(全然背包:入门题) http://acm.hdu.edu.cn/showproblem.php?pid=1248 题意: 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票 ...
- HDU 4508 湫湫系列故事——减肥记I(全然背包)
HDU 4508 湫湫系列故事--减肥记I(全然背包) http://acm.hdu.edu.cn/showproblem.php?pid=4508 题意: 有n种食物, 每种食物吃了能获得val[i ...
- A_全然背包
/* copyright: Grant Yuan algorithm: 全然背包 time : 2014.7.18 __________________________________________ ...
- nyist oj 311 全然背包 (动态规划经典题)
全然背包 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描写叙述 直接说题意,全然背包定义有N种物品和一个容量为V的背包.每种物品都有无限件可用.第i种物品的体积是c,价值是 ...
随机推荐
- Oracle常见的查询代码
/** * 分页查询 */ int currentPage=3;//当前页码 int pageSize=5;//每页的记录条数 String sql=" select * from &quo ...
- spring---aop(5)---Spring AOP的配置的背后的配置
写在前面 Spring AOP中Pointcut,dvice 和 Advisor三个概念 1)切入点 Pointcut 在介绍Pointcut之前,有必要先介绍 Join Point(连接点)概念. ...
- [Dynamic Language] Python非子包引用
Python非子包引用 python的搜索路径其实是一个列表(sys.path) 导入模块时python会自动去找搜索这个列表当中的路径,如果路径中存在要导入的模块文件则导入成功. 在项目中如果要引用 ...
- Using PWM Output as a Digital-to-Analog Converter
http://www.ti.com/lit/an/spraa88a/spraa88a.pdf http://www.ti.com/litv/zip/spraa88a The high-resoluti ...
- HDU4548+筛素数
先筛出素数,再筛出美素数. 简单题. /* 筛素数 */ #include<stdio.h> #include<string.h> #include<stdlib.h&g ...
- RobotFramework自动化4-批量操作案例
前言 有时候一个页面上有多个对象需要操作,如果一个个去定位的话,比较繁琐,这时候就可以定位一组对象.Selenium2library提供了Get Webelements 关键字,用于定位一组元素 以百 ...
- HTML5中的Web Storage(sessionStorage||localStorage)理解与简单实例
Web Storage是什么? Web Storage功能,顾名思义,就是在Web上针对client本地储存数据的功能,详细来说Web Storage分为两种: sessionStorage: 将数据 ...
- Android之Volley使用
转自:http://blog.csdn.net/lfdfhl/article/details/12223345 稍微做了一点儿修改 /** * 利用NetworkImageView显示网络图片 */ ...
- 结合MapReduce和数据集Combining datasets with MapReduce
While in the SQL-world is very easy combining two or more datasets - we just need to use the JOIN ke ...
- UT-Austin大学在Image search and large-scale retrieval方面的一系列papers
WhittleSearch: Interactive Image Search with Relative Attribute Feedback. A. Kovashka, D. Parikh, a ...