Coin Change
Time Limit: 1000/1000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7813Accepted Submission(s): 2592
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
#include<stdio.h>
#include<string.h>
int c1[251][101],c2[251][101];//次数顶多为250 前一个括号是指系数 后一个指硬币个数
int main()
{
int i,j,k,n,l,sum;
int val[6]={0,1,5,10,25,50};
memset(c1,0,sizeof(c1));
memset(c2,0,sizeof(c2));
for(i=0;i<=100;i++)
{
c1[i][i]=1;
}
n=250;
for(i=2;i<=5;i++)
{
for(j=0;j<=n;j++)
for(k=0;k+j<=n;k+=val[i])//次方最大为k+j所以要控制它小于n
for(l=0;l+k/val[i]<=100;l++)
c2[k+j][l+k/val[i]]+=c1[j][l];
for(k=0;k<=n;k++)
for(l=0;l<=100;l++)
{
c1[k][l]=c2[k][l];
c2[k][l]=0;
}
}
while(scanf("%d",&n)!=EOF)
{
sum=0;
for(i=0;i<=100;i++)
sum+=c1[n][i];
printf("%d\n",sum);
}
// }
return 0;
}
#include<string.h>
int c1[251][101],c2[251][101];//次数顶多为250 前一个括号是指系数 后一个指硬币个数
int main()
{
int i,j,k,n,l,sum;
int val[6]={0,1,5,10,25,50};
memset(c1,0,sizeof(c1));
memset(c2,0,sizeof(c2));
for(i=0;i<=100;i++)
{
c1[i][i]=1;
}
n=250;
for(i=2;i<=5;i++)
{
for(j=0;j<=n;j++)
for(k=0;k+j<=n;k+=val[i])//次方最大为k+j所以要控制它小于n
for(l=0;l+k/val[i]<=100;l++)
c2[k+j][l+k/val[i]]+=c1[j][l];
for(k=0;k<=n;k++)
for(l=0;l<=100;l++)
{
c1[k][l]=c2[k][l];
c2[k][l]=0;
}
}
while(scanf("%d",&n)!=EOF)
{
sum=0;
for(i=0;i<=100;i++)
sum+=c1[n][i];
printf("%d\n",sum);
}
// }
return 0;
}
此题给了一个思路 就是如何去求一个n拆分成各种硬币 一共用了多少个硬币
本文介绍了一种解决硬币找零问题的算法实现,通过动态规划的方法计算出给定金额下不同硬币组合的数量。该算法能够处理多达100枚硬币的情况,并提供了完整的C语言代码示例。
1055

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



