Coin Change
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 16135 Accepted Submission(s): 5485
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.
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题目要求硬币数目不能超过100! 所以要加一个限制条件!代码:#include<stdio.h> #include<string.h> #define N 250+10 #define MAX 260 int sum[N]; int coin[6]={0,1,5,10,25,50}; void dabiao() { int i,j,k,t; int p;//表示当前已经用过的硬币数量 int c1[N][N],c2[N][N]; memset(c1,0,sizeof(c1)); memset(c2,0,sizeof(c2)); memset(sum,0,sizeof(sum)); for(i=0;i<=100;i++)//初始化 c1[i][i]=1; for(i=2;i<=5;i++) { for(j=0;j<MAX;j++) { for(k=0,p=0;k+j<MAX;k+=coin[i],p++) { for(t=0;t+p<=100;t++)//限制硬币数目 { c2[k+j][t+p]+=c1[j][t]; } } } for(j=0;j<MAX;j++)//进行更新 for(k=0;k<=100;k++) { c1[j][k]=c2[j][k]; c2[j][k]=0; } } for(i=0;i<MAX;i++) { for(j=0;j<=100;j++) { sum[i]+=c1[i][j]; } } } int main() { dabiao();//打表 int n; while(scanf("%d",&n)!=EOF) { printf("%d\n",sum[n]); } return 0; }