Java Application Simple recursion

Source: Internet
Author: User

After graduation on how to learn the algorithm, still in school when learning data structure, and now basically back to the teacher, but the teacher tuition fees did not return me ...

Scenario: Like given a number, count how many numbers he consists of, such as: 36 now has 10, 5, 1, then the best handsome 3 10, a 5, a 1 composition (the default priority to take maximum). Because we need to develop a business on this side of the ticket, and sometimes do not want a count, let us write a. But the number of his invoice is not fixed, endless, so every time to judge, if: 36 now the base is still 10, 5, 1 but only let you take 1 10, 2 5, a number of 1, then the combination should be: a 10, 2 5, 16 1, probably this meaning.

Add two lists, one to hold the cardinality (from large to small) and the corresponding number of the base to hold.

List List =NewArrayList (); List.add (10); List.add (100); List.add (1); List.add (20); List.add (50); List.add (5);        Collections.sort (list, Collections.reverseorder ()); List List2=NewArrayList (); List2.add (1);//100 corresponding number (hereinafter)List2.add (4);// -List2.add (1);// -List2.add (20);//TenList2.add (50);//5List2.add (1000);//1

Here's the recursion. No difficulty. The parameters are: the total number of inputs, the initial subscript of the set, the base set, the number of cardinality sets

 Public voidCintTotle,intflag, List List, List list2) {        //System.out.println ("current Total" + Totle + ", current quota" + list.get (flag));        intsum = 0;  for(inti = 0; I < list.size (); i++) {sum+= (int) List.get (i) * (int) List2.get (i); }        if(Sum <totle) {System.out.println ("Invoice total is not enough, please add invoice!!!!" "); } Else {            if(Totle% (int) list.get (flag) = = 0) {                ints = totle/(int) list.get (flag); //System.out.println (S + "");                if(S > (int) {list2.get (flag)) {System.out.println ("Need" + list.get (flag) + "yuan Invoice" + S + "Zhang, currently only" + list2.get (flag) + "Zhang, all use! "); Totle-= (int) list2.get (flag) * (int) list.get (flag);                    System.out.println (Totle); C (Totle,++flag, list, list2); } Else{System.out.println ("Need" + list.get (flag) + "yuan Invoice" + S + "Zhang"); }            } Else {                ints = totle/(int) list.get (flag); if(S > (int) {list2.get (flag)) {System.out.println ("Need" + list.get (flag) + "yuan Invoice" + S + "Zhang, currently only" + list2.get (flag) + "Zhang, all use! "); Totle-= (int) list2.get (flag) * (int) list.get (flag);                    System.out.println (Totle); C (Totle,++flag, list, list2); } Else{totle= Totle% (int) list.get (flag); System.out.println ("Need" + list.get (flag) + "yuan Invoice" + S + "Zhang");                    System.out.println (Totle); C (Totle,++flag, list, list2); }            }        }    }

Assume:

C (365, 0, list, list2);

Output

If the input exceeds all of the total, then the following:

Very simple, record, but also remind yourself, the algorithm is quite important ... Do not have to follow slowly study!!!

Java Application Simple recursion

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.