357-let Me Count the Ways
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_ problem&problem=293
After making a purchase at a large department store, Mel's change was cents. He received 1 dime, 1 nickel, and 2 pennies. Later that day, he is shopping at a convenience store. Again his change is cents. This is received 2 nickels and 7 pennies. He began to wonder ' how many stores can I shop in and receive cents change in a different configuration of coins? After a suitable mental struggle, he decided the answer was 6. He then challenged your to consider problem.
Write a program which'll determine the number of different combinations of US coins (penny:1c, nickel:5c, dime:10c, q UARTER:25C, half-dollar:50c) which may is used to produce a given amount of.
Input
The input would consist of a set of numbers between 0 and 30000 inclusive, one per line in the input file.
Output
The output would consist of the the appropriate statement from the selection below on a single in the output file for each Input value. The numberm is the number your program computes and n is the input value.
There are mways to produce ncents change.
There is only 1 way to produce ncents the change.
Sample input
4
Sample output
There are 6 ways to produce cents change.
There are 4 ways to produce one cents change.
There is only 1 way to produce 4 cents change.
Idea: dp[i] + = dp[i-coin[k];
Note A separate state transfer for each coin[k].
Note that it will be super Int.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
Complete code:
/*0.019s*/
#include <cstdio>
#include <cstring>
const int Coin[5] = {1, 5, A, n};
const int MAXN = 30001;
Long long DP[MAXN];
int main ()
{
int i, k, N;
Dp[0] = 1;
for (k = 0; k < 5; ++k)
for (i = coin[k]; i < MAXN ++i)///Note the order of state transitions
dp[i] + = dp[i-coin[k]];
while (~SCANF ("%d", &n))
{
if (n < 5) printf ("There are only 1 way to produce%d cents change.\n", n);
else printf ("There are%lld ways to produce%d cents change.\n", dp[n], n);
return 0;
}