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

    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
    dp[i]=sum(dp[i-1],dp[i-5],dp[i-10],dp[i-20],dp[i-50]),但是很容易想到会算重复,但是在背包里就说了一种避免重复的方法,下面给出两个程序
    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    using namespace std;
    int dp[8000];
    int st[5]={1,5,10,25,50};
    int main()
    {
        int n;
        while (scanf("%d", &n))
        {
            memset(dp,0,sizeof(dp));
            dp[0]=1;
            for (int i=0;i<=n;i++)
            for (int j=0;j<5;j++)
            dp[i]+=dp[i-st[j]];
            printf("%d
    ",dp[n]);
        }
        return 0;
    }

    这个程序看上去不错,不过它真的是错的,在计算的时候重复计算了很多,下面再给出一个程序

    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    using namespace std;
    int dp[8000];
    int st[5]={1,5,10,25,50};
    int main()
    {
        memset(dp,0,sizeof(dp));
        dp[0]=1;
        for (int i=0;i<5;i++)
            for (int j=st[i];j<=7490;j++)
            dp[j]+=dp[j-st[i]];
        int n;
        while (scanf("%d", &n)!=EOF)
        {
            printf("%d
    ",dp[n]);
        }
        return 0;
    }

    而把循坏顺序变一下,结果就不一样了,这样就对了,上面这个程序跑了将近15ms,下面再给出一个

    #include<iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    using namespace std;
    int dp[8000];
    int st[5]={1,5,10,25,50};
    int main()
    {
        int n;
        while (scanf("%d", &n)!=EOF)
        {
            memset(dp,0,sizeof(dp));
            dp[0]=1;
            for (int i=0;i<5;i++)
            for (int j=st[i];j<=n;j++)
            dp[j]+=dp[j-st[i]];
            printf("%d
    ",dp[n]);
        }
        return 0;
    }

    而这个程序跑了将近2s,差别啊!!!是不是很神奇啊

    至少做到我努力了
  • 相关阅读:
    报表容器元素的典型用途:保持位置关系+表角斜线
    如何在Wyn仪表板中实现文件下载
    报表表格中的迷你图
    怎么实现固定行数的表格类报表
    如何把自己设计好的仪表板分享给别人?
    报表中的【子弹图】实现方法
    来自不同数据集的图表如何设置联动
    仪表板图表条件格式化设置
    报表中的数据格式设置方法汇总
    仪表板数据表根据用户需求可以自由选择查看的数据列
  • 原文地址:https://www.cnblogs.com/chensunrise/p/3705472.html
Copyright © 2011-2022 走看看