1. Problem Description
For the m face value v1, v2, vm coins, composed of money n, does not limit the number and location of the coins, these coins demand, many combinations of the results up to exactly equal to n.
2. Analysis
n = x1 * v1 + x2 * v2 + .... + xm * vm, {x1, x2, ...., xm} is one of a combination define l [i] [k] is a front coin i the total number of the composition of the combination of money k
l[m][n]+=l[m-1][n-k*coinsValue[m]]
3. code implementation
class public CoinsTest { public static int coinTest ( int [] coinValues, int n-) { // definition of Matrix [i] [sum] i is the number of combinations is the combination of sum currencies int [] [] Matrix = new new int [. coinValues length + . 1 ] [n-+ . 1 ]; for ( int I = 0 ;. I <coinValues length + . 1 ; I ++) { for ( int J = 0 ; J <n-+ . 1 ; J ++) { Matrix [I] [ J] = 0 ; } } for(int i=0;i<coinValues.length+1;i++) { matrix[i][0]=1; } for(int j=1;j<n+1;j++) { matrix[0][j]=0; } for(int i=1;i<coinValues.length+1;i++) { for(int j=1;j<n+1;j++) { for(int k=0;k<=n/coinValues[i-1];k++) { matrix[i][j]+=matrix[i-1][n-k*coinValues[i-1]]; } } } return matrix[coinValues.length][n]; } public static void main(String[] args) { int [] coinValues={1,2,5}; int combinations=coinTest(coinValues,10); System.out.println("组合数位:"+combinations); }
}
operation result: