UVa 357 Let Me count the Ways: classic dp& coin combination number & integer split

Source: Internet
Author: User
Tags count printf split time limit

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;  
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.