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


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

  • 相关阅读:
    perlsplice
    perl中数组函数:delete和grep
    Python字符串格式化
    blast命令解释
    通俗解释托管与非托管
    四、GO语言的转义字符
    六、GO语言的指针
    五、GO语言的变量及数据类型
    一、GO语言的特点
    前台生成验证码案例
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4666233.html
Copyright © 2011-2022 走看看