Coin Change UVA
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, two 5-cent coins and one 1-cent coin, 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 7489 cents.
Input
The input file contains any number of lines, each one consisting of a number 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
这个题目的状态转移方程为dp [ j ] =sum(dp [ j - w [ i ] ] ,dp[ j ]) ;,属于多重背包问题
#include<iostream>
using namespace std;
typedef long long ll;
const int N =+;
ll dp[N];
int w[]={,,,,,};//相当与有5个物品,可以无限制的取出,每个物品的价值为w[i],,
void inin(){
dp[]=;
for(int i=;i<=;i++){
for(int j=w[i];j<=N;j++)
dp[j]+=dp[j-w[i]];
}
}
int main(){
inin();
int n;
while(cin>>n)
cout<<dp[n]<<endl;
return ;
}
洛谷网校有个题目和这个特别类似
连接在这里:https://www.luogu.org/record/21912220(这个是01背包的)状态转移方程也为(dp [ j ] =sum(dp [ j - w [ i ] ] ,dp[ j ]))
题目背景
uim神犇拿到了uoi的ra(镭牌)后,立刻拉着基友小A到了一家……餐馆,很低端的那种。
uim指着墙上的价目表(太低级了没有菜单),说:“随便点”。
题目描述
不过uim由于买了一些辅(e)辅(ro)书,口袋里只剩MM元(M \le 10000)(M≤10000)。
餐馆虽低端,但是菜品种类不少,有NN种(N \le 100)(N≤100),第ii种卖a_iai元(a_i \le 1000)(ai≤1000)。由于是很低端的餐馆,所以每种菜只有一份。
小A奉行“不把钱吃光不罢休”,所以他点单一定刚好吧uim身上所有钱花完。他想知道有多少种点菜方法。
由于小A肚子太饿,所以最多只能等待11秒。
输入格式
第一行是两个数字,表示NN和MM。
第二行起NN个正数a_iai(可以有相同的数字,每个数字均在10001000以内)。
输出格式
一个正整数,表示点菜方案数,保证答案的范围在intint之内。
输入输出样例
4 4
1 1 2 2
3
#include<iostream>
#include<cstdio>
using namespace std;
const int N=1e5+;
int dp[N];
int w[N];
int main()
{
int n,m;
cin>>n>>m;
for(int i=;i<=n;i++){
cin>>w[i];
} dp[]=;
for(int i=;i<=n;i++){
for(int j=m;j>=w[i];j--)
dp[j]+=dp[j-w[i]];
} cout<<dp[m]<<endl;
return ;
}
对着这一类的问题的总结:这一类问题是背包问题里的解决方案的个数状态方程为dp[ j ] = sum(dp[ j ] , dp[j-w[ i ] ])
就是在背包容量为m下的解决方案的个数 直接累加就可以了。
https://www.cnblogs.com/Accepting/p/11278384.html 这个是背包问题中 最小值问题
Coin Change UVA的更多相关文章
- E - Coin Change UVA - 674 &&(一些记录路径的方法)
这一道题并不难,我们只需要将dp数组先清空,再给dp[0]=1,之后就按照完全背包的模板写 主要是我们要证明着一种方法不会出现把(1+3+4)(1+4+3)当作两种方法,这一点如果自己写过背包的那个表 ...
- UVA 674 Coin Change(dp)
UVA 674 Coin Change 解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/ ...
- UVA.674 Coin Change (DP 完全背包)
UVA.674 Coin Change (DP) 题意分析 有5种硬币, 面值分别为1.5.10.25.50,现在给出金额,问可以用多少种方式组成该面值. 每种硬币的数量是无限的.典型完全背包. 状态 ...
- [LeetCode] Coin Change 硬币找零
You are given coins of different denominations and a total amount of money amount. Write a function ...
- HDOJ 2069 Coin Change(母函数)
Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 2069 Coin Change
Coin Change Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- JSU省赛队员选拔赛个人赛1(Coin Change、Fibbonacci Number、Max Num、单词数、无限的路、叠筐)
JSU省赛队员选拔赛个人赛1 一.题目概述: A.Coin Change(暴力求解.动态规划) B.Fibbonacci Number(递推求解) C.Max Num(排序.比较) D.单词数 ...
- C - Coin Change (III)(多重背包 二进制优化)
C - Coin Change (III) Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- Coin Change (IV) (dfs)
Coin Change (IV) Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu [Subm ...
随机推荐
- Java ServerSocket详解
ServerSocket 构造方法 ServerSocket serverSocket = new ServerSocket(); ServerSocket(); //无参数 ServerSocket ...
- java两数相乘基础算法
下面是别人给我的代码: package com.bootdo; public class Test { public static void main(String[] args) { System. ...
- html之锚点
我命由我不由天 豆瓣:哪吒之魔童降世 哪吒之魔童降世简述 更新描述或海报 导演: 饺子 编剧: 饺子 / 易巧 / 魏芸芸 主演: 吕艳婷 / 囧森瑟夫 / 瀚墨 / 陈浩 / 绿绮 / 更多... ...
- JDK的sql设计不合理导致的驱动类初始化死锁问题
问题描述 当我们一个系统既需要mysql驱动,也需要oracle驱动的时候,在并发加载初始化这些驱动类的过程中产生死锁的可能性非常大,下面是一个模拟的例子,对于Thread2的实现其实是jdk里jav ...
- .Net Web Api返回Json数据中原对象变量名大小写问题
这两天在工作中使用SignalR的WebSocket做数据实时传递的功能开发,在后端主动向前端广播数据以Json传递时,前端获取的Json中对应类的变量名首字母默认传递的是大写.而前端一直获取到的后台 ...
- POJ2182 Lost Cows 题解
POJ2182 Lost Cows 题解 描述 有\(N\)(\(2 <= N <= 8,000\))头母牛,每头母牛有自己的独一无二编号(\(1..N\)). 现在\(N\)头母牛站成一 ...
- 【python系统学习12】函数
函数 函数是一堆组织好的.可重复利用的.用来实现某一功能的代码. python中的input().print().type().bool().len()等都是函数.且是python的内置函数. 我们也 ...
- SQL实战(三)
一. 查找所有员工自入职以来的薪水涨幅情况,给出员工编号emp_noy以及其对应的薪水涨幅growth,并按照growth进行升序CREATE TABLE `employees` (`emp_no` ...
- 前端之jQuery基础篇
jQuery 是一个 JavaScript 库. jQuery 极大地简化了 JavaScript 编程. jQuery 很容易学习. jQuery 安装 网页中添加 jQuery 可以通过多种方法在 ...
- 【Net】ABP框架学习之它并不那么好用
前言 上一篇文章介绍了ABP的Web API,本文在继续介绍ABP的其他内容. 在ABP中,WEBAPI是一个值得用的东西.但其他东西,就不一定是那么好用了. 下面我们看一下ABP的Controlle ...