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
#include <stdio.h>
#include <stdlib.h>
int a[260][101],b[260][101];//第二维数组为硬币数
int main()
{
int i,j,k,l,n,sum;
int c[5]={1,5,10,25,50};
for(i=0;i<=260;i++)
for(j=0;j<=101;j++)
{
a[i][j]=0;b[i][j]=0;
}
for(i=0;i<=100;i++)//用面值为1的硬币组成价值为i(不超过100)
{
b[i][i]=1;
}
for(i=1;i<5;i++)
{
for(j=0;j<=260;j++)
{
for(k=0;j+k<=260;k+=c[i])
{
for(l=0;(l+k/c[i])<=100;l++)
a[k+j][l+k/c[i]]+=b[j][l];
}
}
for(j=0;j<=251;j++)//一定是250 否则过不了 不知道为什么
for(l=0;l<=100;++l)
{
b[j][l]=a[j][l];
a[j][l]=0;
}
}
while(scanf("%d",&n)!=EOF)
{
sum=0;
for(i=0;i<=100;i++)
sum+=b[n][i];
printf("%d\n",sum);
}
return 0;
}
本文介绍了一种解决硬币找零问题的算法实现,通过动态规划的方法找出组成任意金额的不同硬币组合方式的数量。该算法可以处理最多100枚硬币的情况。
2256

被折叠的 条评论
为什么被折叠?



