• 题解报告:hdu 2069 Coin Change(暴力orDP)


    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2069

    Problem Description

    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, or two 5-cent coins and one 1-cent coin, or 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 100 coins.

    Input

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

    解题思路:这道题可以用暴力枚举直接解决。枚举每种硬币的数量为0~n/这种币值即可,为了避免TLE超时,最后一种硬币换成表达式来判断,用num来计数情况,其中注意所有币值的总数量<=100。

    AC代码一(直接暴力):

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int main()
     4 {
     5     int n,num;
     6     while(cin>>n){
     7         num=0;
     8         for(int a=0;a*50<=n;a++){
     9             for(int b=0;b*25<=n;b++){
    10                 for(int c=0;c*10<=n;c++){
    11                     for(int d=0;d*5<=n;d++){//剩下一步由减法来,避免超时
    12                         if(n-a*50-b*25-c*10-d*5>=0 && a+b+c+d+n-a*50-b*25-c*10-d*5<=100)num++;
    13                     }
    14                 }
    15             }
    16         }
    17         cout<<num<<endl;
    18     }
    19     return 0;
    20 }

     AC代码二之dp:先贴一下此题的思路:题解报告:hdu 1284 钱币兑换问题(简单数学orDP)这题就是多加了一个维度,因为题目中规定了硬币的数量最多取100个,因此定义dp[k][j]表示前k个硬币组成钱j的总方案数,那么易得状态转移方程:dp[k][j]+=dp[k-1][j-a[i]],意思是减去当前某种一个币值,那么就会增加前k-1个硬币组成钱j-a[i]的方案数dp[k-1][j-a[i]](对于同一种硬币a[i]来讲)。预处理打表,然后累加用k(k∈[0,100])个硬币组成钱n的所有方案数即为最终的方案总数。注意:①初始化dp[0][0]=1,表示前0个硬币组成钱0的方案数为1(原因和上面链接博文里的一样)答案要累加不超过i:0-->100得到的总方案数,一定要从0开始累加,因为有0个硬币时的方案数。

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int main(){
     4     int n,sum,a[5]={1,5,10,25,50},dp[105][260];
     5     memset(dp,0,sizeof(dp));dp[0][0]=1;
     6     for(int i=0;i<5;++i)//种数
     7         for(int k=1;k<=100;++k)//硬币总数不超过100
     8             for(int j=a[i];j<=250;++j)
     9                 dp[k][j]+=dp[k-1][j-a[i]];
    10     while(cin>>n){
    11         sum=0;
    12         for(int k=0;k<=100;++k)sum+=dp[k][n];//累加用0~100组成钱n的所有方案数
    13         cout<<sum<<endl;
    14     }
    15     return 0;
    16 }
  • 相关阅读:
    蘑菇街2016校园招聘第一个编程题
    杭州天丽笔试题
    2016校园招聘 cvte一面被问到的面试题目
    C#中ArrayList,List,数组的区别
    大白话讲解.NET中挂起线程的Thread.Sleep()方法
    OutLook邮箱获取授权码
    EPIC全速下载游戏加速教程
    远程服务器提示:身份验证错误 要求的函数不受支持解决办法汇总-- Windows远程桌面连接
    华硕主板重装系统以后BIOS设置的启动项就没有了,开机就进BIOS,不进系统
    Windows10访问共享文件夹:此用户无法登录,因为该账户当前已被禁用此用户无法登录,因为该账户当前已被禁用或者直接找不到相应的计算机解决办法
  • 原文地址:https://www.cnblogs.com/acgoto/p/8692481.html
Copyright © 2020-2023  润新知