zoukankan      html  css  js  c++  java
  • 背包的硬币问题

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

    HDU 1284 

    #include <iostream>
    using namespace std;
    
    const int M = 32768 + 10;
    
    int dp[M];
    
    int main()
    {
        int n;
        while (~scanf("%d", &n))
        {
            memset(dp, 0, sizeof(dp));
            dp[0] = 1;
            for (int i = 1; i <= 3; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    dp[j] += dp[j - i];
                }
            }
    
            printf("%d
    ", dp[n]);
        }
        return 0;
    }

    HDU2069 

    给你五种硬币:1,5,10,25,50,现在给出一个n,求出用这些组成价值n的种类数

    特别注意:使用硬币数不能超过100

    /**
      *@xiaoran
      *dp[i],最多100枚硬币
      */
    #include<iostream>
    #include<cstdio>
    #include<map>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #include<stack>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #define LL long long
    using namespace std;
    const int a[5]={1,5,10,25,50};
    LL dp[255][101];//dp[j][k]:用k个硬币组成j值的个数
    int main()
    {
        int n;
        while(cin>>n){
            //cout<<res[n]<<endl;
            memset(dp,0,sizeof(dp));
            dp[0][0]=1;
            for(int i=0;i<5;i++){
                for(int k=1;k<=100;k++){//k个硬币
                    for(int j=a[i];j<=n;j++){
                        dp[j][k]+=dp[j-a[i]][k-1];
                    }
                }
            }
            int res=0;
            for(int i=0;i<=100;i++){
                res+=dp[n][i];
            }
            cout<<res<<endl;
        }
        return 0;
    }
  • 相关阅读:
    Java统计程序运行时间(转)
    有符号定点数的表示方法
    移位运算符
    索引
    self与super的区别(转)
    Java经典题型(未完成)
    ObjectiveC 的 self 和 super 详解
    边界计算与不对称边界
    各种排序总结
    运算符的优先级
  • 原文地址:https://www.cnblogs.com/Aragaki/p/7565588.html
Copyright © 2011-2022 走看看