Coin Change
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14857 Accepted Submission(s): 5024
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
考察知识点:二维母函数。
//考查知识点:简单母函数进阶版----二维母函数 表示苦涩难懂。。。 /********************************************************************************************** 二维母函数的模板大致如下: int c1[maxn][maxe],c2[maxn][mare]; int a[num]; 第一步:简单初始化 memset(c1,0,sizeof(c1)); memset(c2,0,sizeof(c2)); 第二步:使计算有循环区间,就是有值可以相加,相加零毕竟无意义。 for(i=0;i<=maxe;++i)//inf为其中的限制条件 c1[i]=1; 第三步:对整个表达式进行逐个两两计算。 for(i=2;i<=num-1;++i)//n-1个表达式,因为第一个与第二个合成的结果记为新的第一个表达式 { for(j=0;j<=n;++j)//对所求的n个表达式之内的值进行计算,j表示前面j个表达式的结果中的第j项 { for(k=0;k+j<=n;k+=a[i]) { for(l=0;l+k/a[i]<=maxe;l++) c2[j+k][l+k/a[i]]+=c1[j][l]; } } for(j=0;j<=n;++j) { for(k=0;k<=maxe;++k) { c1[j][k]=c[j][k]; c2[j][k]=0; } } } 第四步:最后n的组成情况为: for(i=0;i<=maxe;++i) sum+=c1[n][i]; printf("%d\n",sum); 使用情况:可以组分的情况有多种,另外设立一个数组,主要的变化是在k的循环进行。 **********************************************************************************/ #include<stdio.h> #include<string.h> int c1[252][102],c2[252][102]; int a[6]={0,1,5,10,25,50}; int main() { int n; while(~scanf("%d",&n)) { if(n==0) { printf("1\n"); continue; } int sum=0; memset(c1,0,sizeof(c1)); memset(c2,0,sizeof(c2)); int i,j,k,l; for(i=0;i<=100;++i) { c1[i][i]=1;//////////////////////// } for(i=2;i<=5;++i) { for(j=0;j<=n;++j) { for(k=0;k+j<=n;k+=a[i]) { for(l=0;l+k/a[i]<=100;l++) c2[j+k][l+k/a[i]]+=c1[j][l];/////////////// } } for(j=0;j<=n;++j) { for(k=0;k<=100;++k) { c1[j][k]=c2[j][k]; c2[j][k]=0; } } } for(i=0;i<=100;++i) sum+=c1[n][i]; printf("%d\n",sum); } return 0; }
法二:当初做的时候没想到-_-||,简单的暴力竟然也能过。。。
//法二:竟然忘了一个最水的办法:直接暴力-_-|| #include<stdio.h> int main() { int i,j,k,l; int n; while(~scanf("%d",&n)) { if(!n) { printf("1\n"); continue; } int sum=0; for(i=n/50;i>=0;i--) { for(j=(n-i*50)/25;j>=0;j--) { for(k=(n-i*50-j*25)/10;k>=0;k--) { for(l=(n-i*50-j*25-k*10)/5;l>=0;l--) { if(i+j+k+l+n-i*50-j*25-k*10-l*5<=100) sum++; } } } } printf("%d\n",sum); } return 0; }