All coins combinatorial problem - dynamic programming hdu2069

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
 
A copy of AC codes:

#include <the iostream>
the using namespace STD;
const int Money = 251;
const int COIN = 101;
int DP [Money] [COIN] = {0}; // DP [I] [J] represents the amount of i, the number of coins j is a method of the type
int value [5] = {1,5,10,25,50 };

Solve void ()
{
// for (int I = 0; I <Money; I ++) Why only where dp [0] [0] = 0, because dp [j] [1] = dp [j] [1 ] + dp [j-value [ i]] [k-1], only the j-value [i] == 0, the order in Canada. 1
// DP [I] [0] =. 1;
DP [0] [ 0] =. 1;
for (int I = 0; I <. 5; I ++)
{
for (int J = value [I]; J <Money; J ++)
{
for (int K =. 1; K <COIN; K ++)
{
DP [J] [K] = DP [J] [K] + DP [J-value [I]] [K-. 1];
}
}
}
}
int main ()
{
Solve ();
int ANS [Money] = {0};
ANS [0] =. 1; // Note that this step must not be forgotten, a particular value
for (int I =. 1; I <Money; I ++)
{
for (int J =. 1; J <COIN; J ++ )
{
ANS [I] DP = [I] [J] + ANS [I];
}
}
int S;
the while (CIN >> S)
cout<<ans[s]<<endl;
return 0;
}

 

Since the beginning of the learning dynamic programming, this coin issues considered problems started. Until now, I understand it is the use of dynamic programming recurrence relations, for a solution value is computed from the need to solve before, I want to know that is similar to the number of third value, we need to let the first number is multiplied by the second number, for example, as we are all familiar with Fibonacci number, but pay attention to the initial value must understand, otherwise recurrence relations will not go. In addition Fibonacci column is the easiest dp, will be slightly more complicated you need a little more than once recursive, like the coin problem that requires recursion 5 times.

Guess you like

Origin www.cnblogs.com/sunjianzhao/p/11371647.html