hdu 2069 Coin Change

Problem 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

 #include
using namespace std;

int n;
const int maxn = 257;
int dp[maxn][107];//i=金额,j=硬币数,dp[i][j]表示方法数量
int a[5] = {1, 5, 10, 25, 50};
int ans[maxn];
int main()
{
	dp[0][0] = 1;

	for (int i = 0;i<5;i++)//枚举硬币种类
	{
		for (int j = 1;j<101;j++)//枚举硬币数
		{
			for (int k = a[i];k<maxn;k++)//枚举金额
				dp[k][j] += dp[k - a[i]][j - 1];
		}
	}

	for (int i = 0; i <= maxn; i++)
		for (int j = 0; j <= 100; j++)
			ans[i] += dp[i][j];

	//freopen("11.txt", "r", stdin);
	int x;
	while(cin>>x)
	{
		cout <<ans[x] << endl;
	}

		return 0;
}

 

你可能感兴趣的:(HDU,动态规划)