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

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;
}



你可能感兴趣的:(c,File,input,each,output,Types)