zoukankan      html  css  js  c++  java
  • UVa 674 Coin Change (经典DP)

    Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

    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, 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 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 7489 cents.

    Input 

    The input file contains any number of lines, each one consisting of a number 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
    

    Sample Output 

    4
    13
    

    题意:有1,5,10,25,50五种硬币,给出一个数字,问又几种凑钱的方式能凑出这个数。

    分析:经典的dp题。。。可以递推也可以记忆化搜索。。。这题题意与HDU2069完全一样,但是相同的代码却提交不了,经常超时,还是看了大神的思路交的。


     1 #include <cstdio>
     2 #include <cstring>
     3 const int MAXN = 8000;
     4 const int coin[5] = {1, 5, 10, 25, 50};
     5 int n;
     6 long long dp[MAXN][5];
     7 
     8 long long solve(int i, int s)
     9 {
    10     if(dp[s][i]!= -1)
    11         return dp[s][i];
    12     dp[s][i]=0;
    13     for(int j=i;j<5 && s>=coin[j];j++)
    14         dp[s][i]+=solve(j,s-coin[j]);
    15     return dp[s][i];
    16 }
    17 
    18 int main()
    19 {
    20     memset(dp,-1,sizeof(dp));
    21     for(int i=0;i<5;i++)
    22         dp[0][i]=1;
    23     while(~scanf("%d",&n))
    24         printf("%lld
    ",solve(0,n));
    25     return 0;
    26 }
  • 相关阅读:
    show-meeting-subject-in-meeting-room-calendar
    前端模块化——彻底搞懂AMD、CMD、ESM和CommonJS
    微软到底有多恐怖?
    Office365云流程与开发授权错误
    JQuery实现省市联动 address-picker
    我的新作品 《平虏灭寇英杰传》
    ZT:C/C++ 字符串与数字相互转换
    C016:字符串倒置
    C015:十进制转8进制
    C014:不用算术分割显示逆序三位数
  • 原文地址:https://www.cnblogs.com/caterpillarofharvard/p/4244871.html
Copyright © 2011-2022 走看看