いくつかのお金で1つのお金を集める方法の数を求めて、反復を使います

1611 ワード


package test3;
/**
 *     :   1 ,5 ,10 ,20 ,50   5     100 ,
 * 		          ?
 *     :   ,      1a+5b+10c+20d+50e = 100       (a,b,c,d,e >= 0)
 * @author yj
 */
public class MoneyTest {
	public static int[] currencyArr = {1,5,10,20,50};//{1,2,5,10,20,50,100};
	public static int sumNumber = 100;//102,7;
	/**
	 * @author   
	 * @param args
	 */
	public static void main(String[] args) {
		int answer = processEquation(currencyArr,0);
		System.out.println(answer);
	}

	//    
	public static int processEquation(int[] arr,int priorValue){
		//    ,    
		int totalNum = 0;
		//    
		int varNumber = arr.length;
		
		if(varNumber == 1){
			if(ifInt(sumNumber-priorValue,arr[0])){
				return 1;
			}else{
				return 0;
			}
		}else{
			for(int i=0;;i++){
				if(arr[0]*i>sumNumber-priorValue){
					break;
				}
				int[] arr2 = recreatArray(arr);
				totalNum += processEquation(arr2,arr[0]*i+priorValue);
			}
		}
		return totalNum;
	}
	
	//      
	private static int[] recreatArray(int[] arr) {
		int[] arr2 = new int[arr.length-1];
		for(int i=0;i<arr2.length;i++){
			arr2[i] = arr[i+1];
		}
		return arr2;
	}

	//      
	public static boolean ifInt(int number,int i){
		if(number%i == 0){
			return true;
		}else{
			return false;
		}
	}
}