您的位置:首页 > 其它

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

2016-05-06 20:35 579 查看
Coin Change

Time Limit: 1000MSMemory Limit: 32768KB64bit 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

11
26


Sample Output

4
13


题意;

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

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

附上《背包九讲》链接 :/article/7677705.html 点击打开链接

附上代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#define INF 0x3f3f3f3f
using 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
<< endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: