HDU2069 & UVA 674 Coin Change(换硬币 dp 入门经典水题,背包问题)

来源:互联网 发布:电脑风扇反转软件 编辑:程序博客网 时间:2024/04/30 03:23

Coin Change
Time Limit: 1000MS
Memory Limit: 32768KB
64bit IO Format: %I64d & %I64u



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

1126
 

Sample Output

413
 

题意;

一共有 n 元钱,现在有  1 、5、10、25、50 元的钱,问你有多少种方式组合成 n 元钱!


换硬币,动态规划入门经典题型,没有看过《背包九讲》的可以看看《背包九讲》,所以我在这里也就不多说了,状态转移方程   dp[j] = dp[j] + dp[ j - a[i] ];


附上《背包九讲》链接 :http://blog.csdn.net/xia842655187/article/details/51334431 点击打开链接


附上代码:

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <vector>#define INF 0x3f3f3f3fusing namespace std;int main(){int n;while(cin >> n){int a[5] = {1,5,10,25,50};long long int dp[8000] = {0};         //  需要用到  long long 结果有可能超出 int 范围dp[0] = 1;for(int i = 0;i < 5;i++){for(int j = a[i];j <= n;j++){dp[j] = dp[j] +dp[j - a[i]];}}cout << dp[n] << endl;}return 0;}





0 0
原创粉丝点击