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 =32768+10; ll dp[N];
int w[6]={0,50,25,10,5,1};//相当与有5个物品,可以无限制的取出,每个物品的价值为w[i],, void inin(){ dp[0]=1; for(int i=1;i<=5;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 0; }
洛谷网校有个题目和这个特别类似
连接在这里: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+7; int dp[N]; int w[N]; int main() { int n,m; cin>>n>>m; for(int i=1;i<=n;i++){ cin>>w[i]; } dp[0]=1; for(int i=1;i<=n;i++){ for(int j=m;j>=w[i];j--) dp[j]+=dp[j-w[i]]; } cout<<dp[m]<<endl; return 0; }
对着这一类的问题的总结:这一类问题是背包问题里的解决方案的个数状态方程为dp[ j ] = sum(dp[ j ] , dp[j-w[ i ] ])
就是在背包容量为m下的解决方案的个数 直接累加就可以了。
https://www.cnblogs.com/Accepting/p/11278384.html 这个是背包问题中 最小值问题