HDU 2069 Coin Change

    技术2025-04-28  15

    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

    1126 Sample Output 413 母函数的题目,直接套模版的话,不好做。这题就属于母函数里面比较怪胎题目。Your program should be able to handle up to 100 coins.如果不仔细看这句话,你可能会因此waN次。为此这题我用了二维数组来存储组合数,其中第一维保存当前组合数,第二维保存当前使用了几个coins,只要不大于100即可!另外,这题需要打表,不然超时,HDU上的题目令人很蛋疼。 具体的话见下面的代码: #include<cstdio> #include<algorithm> using namespace std; #define N 1000+10 int arr[6]={0,1,5,10,25,50}; int curnum[N][N],sumnum[N][N]; int main() { freopen("zx.in","r",stdin); freopen("zx.out","w",stdout); int num; memset(curnum,0,sizeof(curnum)); memset(sumnum,0,sizeof(sumnum)); for(int i=0;i<=250;i++) sumnum[i][i]=1; for(int i=2;i<=5;i++) { for(int j=0;j<=250;j++) { for(int k=0;k+j<=250;k+=arr[i]) { for(int t=0;t+k/arr[i]<=100;t++) curnum[k+j][t+k/arr[i]]+=sumnum[j][t]; } } for(int j=0;j<=250;j++) { for(int t=0;t<=100;t++) { sumnum[j][t]=curnum[j][t]; curnum[j][t]=0; } } } while(scanf("%d",&num)!=EOF) { int sum=0; for(int i=0;i<=100;i++) sum+=sumnum[num][i]; printf("%d/n",sum); } return 0; }
    最新回复(0)