hdu 2069 母函数(一) 抑或 暴力枚举

本文介绍了一种解决硬币找零问题的算法,通过列举所有可能的组合来找出特定金额下不同类型的硬币找零方式的数量。提供了两种实现方案:一种是基于暴力搜索的方法,另一种则是利用母函数进行优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Coin Change

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6775    Accepted Submission(s): 2246


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
 



先上暴力AC:


#include<iostream>
using namespace std;
int main(){
	int n,a,b,c,d,e;
	while(cin>>n){  
		int count=0;
	for(a=0;a<=n;a++){
		for(b=0;5*b<=n-a;b++){
			for(c=0;10*c<=n-a-5*b;c++){
				for(d=0;25*d<=n-a-5*b-10*c;d++){
					 e=n-a-5*b-10*c-25*d;          //这里的50分必须省去不必要的循环(否则超时) 存在的可否在于下面:
					if(e%50==0&&a+b+c+d+e/50<=100) //e%50==0 要么有50分刚好成n元 要么没有50分  * 别忘了50分的个数是 e/50 *

						count++;
					}
				}
			}
		}
	cout<<count<<endl;
	}
return 0;
}

下面是母函数的:

#include<iostream>
using namespace std;
	int c1[251][101],c2[251][101];
	int a[6]={0,1,5,10,25,50};
	int sum[251];
int main(){
	

	c1[0][0]=1;   
 /* 注意 这里的初始化替代了:
int n,a[5]={1,5,10,25,50},sum; 
    for(int i=0;i<=260;++i)
    for(int j=0;j<=101;++j) 
    {
        c1[i][j]=0;c2[i][j]=0;
    }
    for(int i=0;i<=100;++i)//用面值为1的硬币组成价值为i(不超过100) 
    {
            c1[i][i]=1;*/

        for(int i=1;i<=5;i++){
		for(int j=0;j<=250;j++){
			for(int k=0;k*a[i]+j<=250;k++){
				for(int t=0;k+t<=100;t++){//遍历c1的coin数量
					c2[k*a[i]+j][t+k]+=c1[j][t];
				}
			}
		}
		for(int i=0;i<251;i++){
			for(int j=0;j<101;j++){
				c1[i][j]=c2[i][j];
				c2[i][j]=0;
			}
		}
	}
	/*int n;
	while(cin>>n){
		int sum=0;
		for(int i=0;i<105;i++){
			sum+=c1[n][i];
		}
		cout<<sum<<endl;
	}
	*/
	 for ( int j = 0; j != 251; ++ j )
           {
                for ( int i = 0; i != 101; ++ i )
                {
                      sum[j] += c1[j][i] ;
                }
           }
           int N;
           while ( cin >> N )
           {
                  
                  cout << sum[N] << endl;
           }
return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值