---minimum number of stamps in the algorithm practice

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.