zoukankan      html  css  js  c++  java
  • 动态规划:凑硬币(中级:动态规划思想体会)

    解题心得:
    1、对于动态规划,并不是简单的套公式,自己的思想是第一位。首先应该自己去想解决问题的方法,用动态规划去理解题,抓住真正的转移点,扩大点,公式会很自然的出来。转移状态的方程式很多变,并不是固定不动的。
    2、此题在动态转移的时候使用的是二维数组,所以方程式是多变的,思想也在变动。
    3、在结果固定不变的题可以先打表,得出所有的答案,在多次询问的时候直接从表中的到结果,这样可以节省时间。
    4、在发现自己的思路不对时,不要用公式去强行套,改变思维。

    题目:
    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

    #include<stdio.h>
    
    int table[7500][5],money,va[5]={1,5,10,25,50};
    void money_table()
    {
            //也可以是面额转移,钱不变,所以是dp灵活的是思想,不是公式。
            for(int j=1;j<7500;j++)//钱逐步扩大,状态转移。
                for(int i=1;i<5;i++)//分别使用5、10、25、50的钱去凑(1元在主函数里已经使用)。
                    for(int k=0;k*va[i]<=j;k++)//注意不能缺少等号。
                        table[j][i] += table[j-k*va[i]][i-1];//1元的加上用5元的加上10元的慢慢的加,最后得到最终的答案。
    }
    int main()
    {
            for(int i=0;i<7500;i++)
                table[i][0] = 1;//当只使用一元的时候,无论多少money都只有一种方法。
        money_table();
        while(scanf("%d",&money)!=EOF)
        {
            printf("%d
    ",table[money][4]);//table[money][4]最后得到的是加到了第50元上。
        }
    }
    
  • 相关阅读:
    隐藏 MOSS 2007 页面版本工具栏
    用于显示原始XML形式的搜索结果的XSLT
    MOSS 2007 日志设置
    在布局页面“文章页面中”添加,自定义UserControl
    MOSS 2007 最简单的自定义搜索框 SearchBox
    Asp.net常用状态管理方案分析
    提高asp.net的性能的几种方法(转)
    VS2005下如何用预编译命令来发布站点
    asp.net控件设计时支持(1)
    解决Enterprise Library January 2006不能加密配置文件的方法(转)
  • 原文地址:https://www.cnblogs.com/GoldenFingers/p/9107386.html
Copyright © 2011-2022 走看看