zoukankan      html  css  js  c++  java
  • 硬币的问题

    不同的表面值Value[ ]有硬币的数量Num[ ]限制,凑齐Goal表面值,的需求的最小和最大数目。

    static int  Min = 1<<10;
    static  int Max = 0;
    static int* set;
    static int* Count;
    
    
    void LeastCoin_N(int* Value, int* Num, int Len, int Goal, int cur) 
    {   
    	if(Goal == 0) 
    	{
    		if(cur > Max)
    		{
    			for(int i = 0; i < Len; i++)
    			{		
    				for(int j = 0; j < cur; j++)
    				{
    					if(Value[i] == set[j])
    					{
    						Count[i]++;
    						if(Count[i] > Num[i])
    						{
    							goto  initial;
    						}
    					}
    
    				}
    			}
    
    			for(int i = 0; i < cur; i++)
    			{
    			     printf("%d ", set[i]);  
    			}
    			Max = cur;
    		}
    		if(Min > cur)
    		{			
    			for(int i = 0; i < Len; i++)
    			{		
    				for(int j = 0; j < cur; j++)
    				{
    					if(Value[i] == set[j])
    					{
    						Count[i]++;
    						if(Count[i] > Num[i])
    						{
    							goto  initial;
    						}
    					}
    
    				}
    			}  
    		
    			for(int i = 0; i < cur; i++)
    			{
    				printf("%d ", set[i]); 
    			}
    			        Min = cur;
    		}
    initial:	
    		memset(Count, 0 , sizeof(int)*Len);
    		printf("
    ");
    	}
    	else
    	{
    		for(int i = 0; i < Len; i++)
    		{
    
    			if(Goal >= Value[i]) 
    			{
    				
    			        int ok = 1;
    				for(int j = 0; j < cur; j++)
    				{
    					if(set[j] > Value[i])
    					{
    						ok = 0;
    						break;
    					}
    					
    				}
    				if(ok)
    				{				
    					set[cur] = Value[i];  
    					LeastCoin_N(Value, Num, Len, Goal - Value[i], cur + 1);				
    				}
    			}		
    
    		}
    
    	}
     
    }
    
    int WLeastCoin_N(int* Value, int* Num, int Len,int Goal)
    {
    
    	printf("goal: %d
    ", Goal);
    
    	set = new int [Len];
    	memset(set, 0 , sizeof(int)*Len);
    	Count = new int [Len];
    	memset(Count, 0 , sizeof(int)*Len);
    
    	int cur = 0;
    	LeastCoin_N(Value, Num, Len, Goal,  cur);
    	printf("Max:%d 
    ", Max);
    	printf("Min:%d 
    ", Min);
    	return 0;
    
    }


    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    线程运行boost库在工作(22)任务之二
    vi 帮助文档 man vi
    跳槽关系三国演义告诉我们的60条真理
    后台端口虚拟主机wdcp的相关问题以及解决方法
    格式化字符串android 格式化时间
    对象查询HQL多表联合查询的问题
    myeclipse8.6中svn插件的安装
    乱码解决方法
    Restfull风格是什么意思?
    poj3013
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4666233.html
Copyright © 2011-2022 走看看