Preface:
About this kind of ingenious permutation combinatorial class algorithm problem, need to find this kind of problem solves the characteristic, then according to this characteristic gradually solves the process. In this question, the minimum number of combinations is required for a solution and a consistent case, where I define an array with a sum length that holds the result value, and a double loop that iterates through the total number of numbers, in which the second layer decrements from the sum value, gradually finding the smallest, and finally the result bit in the output array.
The title describes a number of stamps, which require a minimum number of stamps to be taken together into a given total value. For example, there are 1 points, 3 points, 3 points, 3 points, 4 points five stamps, which require 10 points, then 3 stamps are used: 3 points, 3 points, 4 points.
Input Description:
There are multiple sets of data, for each group of data, the first is the demand for a m,m<100 stamp value. Then there is a number n,n〈20, which indicates that there are N stamps. Next is a n positive integer representing the face value of the n stamps, in ascending order.
Output Description:
For each set of data, it is possible to sum up the minimum number of stamps in total M. If there is no solution, output 0.
Input Example:
10
5
1 3 3) 3 4
Output Example:
3
ImportJava.util.Scanner; Public classminimum number of stamps { Public Static voidMain (string[] args) {Scanner in=NewScanner (system.in); while(In.hasnext ()) {intsum=In.nextint (); intCount =In.nextint (); int[] num=New int[Count]; for(inti=0;i<count;i++) {Num[i]=In.nextint (); } System.out.println (Mincount (num,count,sum)); } in.close (); } Private Static intMincount (int[] num,intCountintsum) { int[] DP =New int[Sum + 1]; for(inti = 1; i < dp.length; i++) {Dp[i]= sum + 1; } for(inti = 0; I < count; i++) { for(intj = sum; J >= Num[i]; j--) {Dp[j]= Math.min (Dp[j], Dp[j-num[i]] + 1); System.out.println (Dp[j]+ "i=" +i+ "" + "j=" +j); } } if(dp[sum]==sum+1) { return0; } returnDp[sum]; }}
---minimum number of stamps in the algorithm practice