zoukankan      html  css  js  c++  java
  • hdu2069(Coin Change)

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

    Coin Change

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 23514    Accepted Submission(s): 8250


    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
     
    Author
    Lily
     
    Source
     
    题目大意:有50,25,10,5,1分的硬币,输入n,代表你要凑成的总数,要求你用这些硬币刚好凑成n,切硬币总个数不超过100
    思路:这题超级大水,额,菜鸡的我以为很难很难,想了很久,绞尽脑汁也没有想出来,没想到直接5重循环就轻易解决了 ,很难受
    看代码吧:
    #include<iostream>
    #include<string.h>
    #include<map>
    #include<cstdio>
    #include<cstring>
    #include<stdio.h>
    #include<cmath>
    #include<ctype.h>
    #include<math.h>
    #include<algorithm>
    #include<set>
    #include<queue>
    typedef long long ll;
    using namespace std;
    const ll mod=1e9;
    const int maxn=250+50;
    const int maxm=1;
    const int maxx=1e4+10;
    const ll maxe=1000+10;
    #define INF 0x3f3f3f3f3f3f
    #define Lson l,mid,rt<<1
    #define Rson mid+1,r,rt<<1|1
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            int ans=0;
            for(int i=0;i*50<=n;i++)
            {
                for(int j=0;j*25<=n;j++)
                {
                    for(int k=0;k*10<=n;k++)
                    {
                        for(int l=0;l*5<=n;l++)
                        {
                            for(int m=0;m<=n;m++)
                            {
                                if(i*50+j*25+k*10+l*5+m==n&&i+j+k+l+m<=100)
                                    ans++;
                            }
                        }
                    }
                }
            }
            cout<<ans<<endl;
        }
        return 0;
    }

     思路2:dp思想,dp[i][j]表示使得价值为i,用了 j  个硬币。  可以把硬币的价值用a[]存起来,dp[i][j]=dp[i][j]+dp[i-a[k]][j-1]   

     具体看代码:

    #include<iostream>
    #include<string.h>
    #include<map>
    #include<cstdio>
    #include<cstring>
    #include<stdio.h>
    #include<cmath>
    #include<ctype.h>
    #include<math.h>
    #include<algorithm>
    #include<set>
    #include<queue>
    typedef long long ll;
    using namespace std;
    const ll mod=1e9;
    const int maxn=250+50;
    const int maxm=1;
    const int maxx=1e4+10;
    const ll maxe=1000+10;
    #define INF 0x3f3f3f3f3f3f
    #define Lson l,mid,rt<<1
    #define Rson mid+1,r,rt<<1|1
    int a[5]={1,5,10,25,50};
    int dp[maxn][110];//dp[i][j]表示使得价值为i,用了j个硬币的种数
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            memset(dp,0,sizeof(dp));//初始化都为0
            int ans=0;
            dp[0][0]=1;
            for(int i=0;i<5;i++)//5种价值的硬币
            {
                for(int j=1;j<=100;j++)//个数
                {
                    for(int k=a[i];k<=n;k++)//最小为当前的a[i],一直遍历到n
                    {
                        dp[k][j]+=dp[k-a[i]][j-1];//从上一个加上使用这一个的情况
                    }
                }
            }
            for(int i=0;i<=100;i++) ans+=dp[n][i];
            cout<<ans<<endl;
        }
        return 0;
    }
    当初的梦想实现了吗,事到如今只好放弃吗~
  • 相关阅读:
    android彻底结束进程的两种方法
    清理软件缓存
    android webview 设置
    创建和使用Android library工程
    如何使Android应用程序获得root权限
    自动安装mysql5.7shell脚本实现与ansible脚本实现
    老生常谈:Asp.net Mvc 3 加载cshtml文件创建自定义分页
    老生常谈: Asp.net Mvc3 加载cshtml文件自定义分页+ 查询
    Razor API翻译
    Asp.net MVC 3使用 Area 小结
  • 原文地址:https://www.cnblogs.com/caijiaming/p/9745120.html
Copyright © 2011-2022 走看看