1. 程式人生 > >hdu2069(Coin Change)

hdu2069(Coin Change)

algo input mat suppose des nsis memset ace ins

題目鏈接: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;
}

hdu2069(Coin Change)