zoukankan      html  css  js  c++  java
  • Coin Change UVA

    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

    这个题目的状态转移方程为dp [ j ] =sum(dp [ j - w [ i ] ] ,dp[ j ])  ;,属于多重背包问题

    #include<iostream>
    using namespace std;
    typedef long long ll;
    const int N =32768+10;
    ll dp[N];
    int w[6]={0,50,25,10,5,1};//相当与有5个物品,可以无限制的取出,每个物品的价值为w[i],, void inin(){ dp[0]=1; for(int i=1;i<=5;i++){ for(int j=w[i];j<=N;j++) dp[j]+=dp[j-w[i]]; } }
    int main(){ inin(); int n; while(cin>>n) cout<<dp[n]<<endl; return 0; }

    洛谷网校有个题目和这个特别类似

    连接在这里:https://www.luogu.org/record/21912220(这个是01背包的)状态转移方程也为(dp [ j ] =sum(dp [ j - w [ i ] ] ,dp[ j ]))

    题目背景

    uim神犇拿到了uoira(镭牌)后,立刻拉着基友小A到了一家……餐馆,很低端的那种。

    uim指着墙上的价目表(太低级了没有菜单),说:“随便点”。

    题目描述

    不过uim由于买了一些辅(e)辅(ro)书,口袋里只剩MM元(M le 10000)(M10000)。

    餐馆虽低端,但是菜品种类不少,有NN种(N le 100)(N100),第ii种卖a_iai(a_i le 1000)(ai1000)。由于是很低端的餐馆,所以每种菜只有一份。

    小A奉行“不把钱吃光不罢休”,所以他点单一定刚好吧uim身上所有钱花完。他想知道有多少种点菜方法。

    由于小A肚子太饿,所以最多只能等待11秒。

    输入格式

    第一行是两个数字,表示NN和MM。

    第二行起NN个正数a_iai(可以有相同的数字,每个数字均在10001000以内)。

    输出格式

    一个正整数,表示点菜方案数,保证答案的范围在intint之内。

    输入输出样例

    输入 #1
    4 4
    1 1 2 2
    
    输出 #1
    3
    #include<iostream>
    #include<cstdio>
    using  namespace std;
    const int N=1e5+7;
    int dp[N];
    int w[N];
    int main()
    {
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++){
            cin>>w[i];
        }
        
        dp[0]=1;
        for(int i=1;i<=n;i++){
            for(int j=m;j>=w[i];j--)
                dp[j]+=dp[j-w[i]];
        }
        
        cout<<dp[m]<<endl;
        return 0;
    }

    对着这一类的问题的总结:这一类问题是背包问题里的解决方案的个数状态方程为dp[ j  ] = sum(dp[ j ]  , dp[j-w[ i ] ])

    就是在背包容量为m下的解决方案的个数 直接累加就可以了。

     https://www.cnblogs.com/Accepting/p/11278384.html 这个是背包问题中  最小值问题

  • 相关阅读:
    IOS添加pch预编译文件
    UITableview控件基本使用
    两种单例模式的写法
    提交app Store审核时,Missing 64-bit support问题的解决办法
    IOS开发NSString与int和float的相互转换以及字符串拼接、NSString、NSData、char* 类型之间的转换
    获取UITableView每行中不同的UITextField输入的内容(例如修改登陆密码)
    UITextField常用属性归纳:文本框样式、文字样式、键盘样式、左右视图样式、清除按钮设置等,iosuitextfield
    UITabBarController(标签栏控制器)
    iOS基础控件UINavigationController中的传值
    IOS开发之多线程
  • 原文地址:https://www.cnblogs.com/Accepting/p/11279704.html
Copyright © 2011-2022 走看看