Hdoj 2069 Coin Change

Source: Internet
Author: User
Tags time limit

Original question:
Coin Change
Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 16352 Accepted Submission (s): 5567

Problem Description
Suppose there is 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 had one cents, then we can make changes with one 10-cent coin and one 1-cent coin, or both 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 is four ways of making changes for one cents with the above coins. Note that we count this there is one of 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 coins.

Input
The input file contains any number of lines, and 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 COI Ns.

Sample Input

11 26

Sample Output

4 13

Author
Lily

Source
Zhejiang University of Technology Network tryouts
Main topic:
Give you 5 denominations of coins, and then give you a value to ask you how many kinds of money to find the method. But. You can use up to 100 of the total number of coins. This is not the same place as the classic coin problem.
Code:

#include <iostream>
using namespace std;
int dp[251][101];
int coin[5]={50,25,10,5,1};
int main ()
{
    Ios::sync_with_stdio (false);
    Dp[0][0]=1;
    for (int i=0;i<5;i++)
    {for (
        int. j=0;j<=250;j++)
        {for
            (int k=0;k<=100;k++)
                 { if (j>=coin[i]&&k>0)
                 dp[j][k]+=dp[j-coin[i]][k-1];}}
    }
    int n;
    while (Cin>>n)
    {
        int sum=0;
        for (int i=0;i<=100;i++)
            sum+=dp[n][i];
            cout<<sum<<endl;
    }
    return 0;
}

Ideas:
The normal unrestricted coin problem is a complete knapsack problem whose transfer equation is
DP[I]+=DP[I-COIN[J]] [where dp[i] stores the amount of money that can be found when the value is I. Now the problem you're looking for in a certain number of constraints its transfer equation is
DP[I][K]+=DP[I-COIN[J]][K-1] where dp[i][k] represents the number of change methods that can be obtained when the number of J coins is used for a given value of I.
Of course, this problem can also be solved with a female function.
Note that dp[0][0]=1

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.