"To be Continued" Java Blue Bridge Cup--algorithm training (1) Recursive framework for typical problems

Source: Internet
Author: User

Preparatory exercises (i)

The question of the full arrangement of letters:

1  PackagePackage1;2 3 //to arrange letters in full order4 ImportJava.util.*;5 6  Public classA1 {7 8     StaticList F (String s) {9List LST =NewVector ();Ten  One         //Export A         if(s.length () = = 1) { - Lst.add (s); -             returnlst; the         } -  -         //The following is the similarity of recursion -         /*vectors are very similar to ArrayList, but vectors are synchronous. */ +          for(inti = 0; I < s.length (); i++) { -             Charx =S.charat (i); +             /*the CharAt () method is used to return the character at the specified index. The index range is from 0 to length ()-1. */ A             //start recursion (from the removed I position element recursion) atList t = f (s.substring (0, I) + s.substring (i + 1)); -  -             /* - * str=str.substring (int beginindex), intercept str from the initial letter length of Beginindex string, the remaining string is assigned to STR; -              *  - * str=str.substring (int beginindex,int in * endIndex); intercepts the string from Beginindex to the end of EndIndex in STR and assigns it to STR; -              */ to              for(intk = 0; K < T.size (); k++) { +Lst.add ("" + x +T.get (k)); -             } the         } *         returnlst; $ Panax Notoginseng     } -  the      Public Static voidMain (string[] args) { +List lst = f ("ABC");//Pass in a string and put the elements all in the list A          for(inti = 0; I < lst.size (); i++) { the System.out.println (Lst.get (i)); +         } -     } $}

Method two: (array)

1  PackagePackage1;2 3 //to arrange letters in full order4 //method Two: Array5 ImportJava.util.*;6 7  Public classA1 {8     //AA: Pending Data9     //K: Consider the current position (array subscript)Ten     Static voidFChar[] AA,intk) { One          A         //Export -         if(k==aa.length-1) { - System.out.println (string.valueof (AA)); the             //The AA of the array type is cast to a string type and output -             return;//K reaches the line, cannot continue recursion, program returns -         } -          +          -         //Recursive similarity +         //Start with the number of K, and then switch to the last number in the array (K starts with the first number) A          for(inti=k;i<aa.length;i++) {//I=k, that is, do not miss the situation of non-exchange at{Chart=aa[k];aa[k]=aa[i];aa[i]=t;}//Temptation -f (aa,k+1);//call k+1; let the latter element -{Chart=aa[k];aa[k]=aa[i];aa[i]=t;}//backtrack, swap back. -             //to be tempted, to backtrack immediately after recursion. -         } -     } in  -      to      +      -      Public Static voidMain (string[] args) { theF ("ABC". ToCharArray (), 0);//K starting with the No. 0 one *         /*ToCharArray (): Converts a string into a character array, allowing for individual manipulation of each character*/ $         }Panax Notoginseng      -}

Topic One:

There are n ants on a slender straight pole 100 centimeters long. They have their heads on the left, some facing to the right.
Each ant can only climb along the pole, at a speed of 1 centimeters per second.
When two ants meet, they will turn around at the same time and crawl in the opposite direction.

Of these ants, 1 ants have a cold. And when they meet with other ants, they will infect the ant with the cold.
Please calculate how many ants have a cold when all the ants are climbing away from the pole.

"Data Format"
The first line enters an integer n (1 < n < 50), which represents the total number of ants.
The next line is n a space-separated integer XI ( -100 < Xi <), the absolute value of Xi, indicating the distance the ant leaves the left end of the pole. A positive value indicates a head to the right, a negative value indicates a head to the left, no 0 values appear in the data, and no two ants occupy the same position. Among them, the first data represents the ants catching a cold.

Requires an output of 1 integers, indicating the number of last cold ants.

For example, enter:
3
5-2 8
The program should output:
1

Again, for example, enter:
5
-10 8-20 12 25
The program should output:
3

Topic (ii)

Xiao Ming likes to build a number of bricks recently. A total of 10 blocks, each building has a number, 0~9.

Rules for building blocks:
Each block is placed on top of the other two blocks, and must be smaller than the number of two blocks below.
At the end of the 4-storey pyramid, all the bricks must be used.

Here are the two qualifying methods:

0
1 2
3 4 5
6 7 8 9

0
3 1
7 5 2
9 8 6 4

Would you please calculate how many of these methods are there?

Topic (iii)


Planet X is sending a 5-man observer mission to the W-Star.
which
A country can send up to 4 people.
A maximum of 2 people can be dispatched in country B.
C countries can send up to 2 people.
D countries can send up to 1 people.
A maximum of 1 people can be sent to the E country.
A maximum of 3 people can be dispatched in F countries.
So how many different combinations of the country will eventually be sent to the W-Star's Observer mission?

Title (iv)

A string of known different letters, to make its full arrangement

Topic (v)


There are repeated letters to remove all combinations of M

For example: "AAABBCCCCCCDD" to take all combinations of 3 letters

Title (vi)

A a 2 2 3 3 4 4, altogether 4 pairs of poker. Please line them up.
Requirements: Two A in the middle there are 1 cards, two 2 has 2 cards, two 3 have 3 cards, two 4 with 4 cards.

Please fill in all the required permutations, the smallest dictionary order.
For example: 22aa3344 is smaller than the a2a23344 dictionary sequence. They are not, of course, the answer to the request.

"To be Continued" Java Blue Bridge Cup--algorithm training (1) Recursive framework for typical problems

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.