hdu2069 母函数应用变形之总硬币数有限制

本文介绍了一种解决硬币找零问题的算法实现,通过动态规划的方法计算出给定金额下不同硬币组合的数量。该算法能够处理多达100枚硬币的情况,并提供了完整的C语言代码示例。

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.

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
题意: 有50 25 10 5 1 的硬币 一共最多有100枚输入n输出有多少种表示方法
#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;
}
此题给了一个思路 就是如何去求一个n拆分成各种硬币 一共用了多少个硬币
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值