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;
    }
  • 相关阅读:
    WCF开发笔记 高版本.Net的坑
    Net Start可以加载驱动
    解决:Windows 2008远程黑屏问题
    Visual Studio 解决方案版本从v12-->v14
    Visual Studio 使用之禁用/启用模板警告
    Windows 10 常用的快捷键及常用指令
    git
    Socket通信原理 很好
    java集合
    JavaEE简介
  • 原文地址:https://www.cnblogs.com/Aragaki/p/7565588.html
Copyright © 2011-2022 走看看