674-coin Change
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_ problem&problem=615
Suppose there are 5 types of coins:50-cent, 25-cent, 10-cent, 5-cent, and 1-cent. We want to make changes with the coins for a given amount of.
For example, if we have 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 to making changes for one cents with the above. Note this we count that there is one way the making change for zero cent.
Write a program to find the total number of different ways of making changes for all amount of money in cents. Your program should is able to handle up to 7489 cents.
Input
The input file contains any number of lines, and each one consisting of a of number for the amount of the.
Output
For each input line, output a line containing the number of different ways of making changes with the above 5 types of COI Ns.
Sample Input
One
26
Sample Output
4
13
Thinking: From the top down analysis, um ... A five-pronged tree.
So simply push it from the bottom up: dp[i] = Dp[i] + dp[i-coin[k]]
Complete code:
/*0.042s*/
#include <cstdio>
#include <cstring>
const int MAXN = 7490;
const int Coin[5] = {1, 5, ten};
int DP[MAXN];
int main (void)
{
int n;
memset (DP, 0, sizeof (DP));
Dp[0] = 1;
for (int k = 0; k < 5, ++k) for
(int i = coin[k]; i < MAXN ++i)
dp[i] + = dp[i-coin[k]];///Various combinations are superimposed
while (~scanf ("%d", &n))
printf ("%d\n", Dp[n]);
return 0;
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/