zoukankan      html  css  js  c++  java
  • HDU 1284 钱币兑换问题 母函数、DP

    题目链接:HDU 1284 钱币兑换问题

    钱币兑换问题

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 5467    Accepted Submission(s): 3123


    Problem Description
    在一个国家仅有1分,2分。3分硬币,将钱N兑换成硬币有非常多种兑法。请你编程序计算出共同拥有多少种兑法。
     

    Input
    每行仅仅有一个正整数N。N小于32768。

     

    Output
    相应每一个输入,输出兑换方法数。
     

    Sample Input
    2934 12553
     

    Sample Output
    718831 13137761
     

    Author
    SmallBeer(CML)
     

    Source
     

    Recommend
    lcy   |   We have carefully selected several similar problems for you:  2159 1248 1203 1231 1249 
     

    思路1:母函数思想,求系数



    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    
    #define maxn 32770
    int n, c1[maxn], c2[maxn];
    void Init()
    {
        for(int i = 0; i <= maxn; i++)
        {
            c1[i] = 1;
            c2[i] = 0;
        }
        for(int i = 2; i <= 3; i++)
        {
            for(int j = 0; j <= maxn; j++)
                for(int k = 0; k+j <= maxn; k+=i)
                    c2[j+k] += c1[j];
            for(int j = 0; j <= maxn; j++)
            {
                c1[j] = c2[j];
                c2[j] = 0;
            }
        }
    }
    int main()
    {
        Init();
        while(~scanf("%d", &n))
            printf("%d
    ", c1[n]);
        return 0;
    }
    


    思路2:DP,后面的钱能够由前面的钱推出。

    代码:

    #include <iostream>
    #include <cstdio>
    using namespace std;
    
    #define maxn 32770
    int n, dp[maxn];
    void Init()
    {
        dp[0] = 1;
        for(int i = 1; i <= 3; i++)
            for(int j = i; j <= maxn; j++)
                dp[j] += dp[j-i];
    }
    int main()
    {
        Init();
        while(~scanf("%d", &n))
            printf("%d
    ", dp[n]);
        return 0;
    }
    




  • 相关阅读:
    正则表达式
    jquery获取(设置)节点的属性与属性值
    Easy UI
    javascript中数组常用的方法
    DOM节点
    Echarts的基本用法
    CSS小结
    草稿1
    CSS基础
    wordbreak:breakall和wordwrap:breakword的区别
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5099117.html
Copyright © 2011-2022 走看看