Java process-nesting and iteration

Source: Internet
Author: User
Tags greatest common divisor

Iteration and nesting are two very useful algorithms for process-oriented processes, and they are also used in some Java development. Learn some fur today and summarize it as follows.

1 recursion and iteration of the Linetype:

Linear process structure is relatively simple, easy to understand, and from the description to the code of writing is easier to implement. The most common is to calculate factorial:

1.1. The idea of iteration is that, starting from 1, each time a new i is multiplied, the result of the new calculation replaces the old result:n->n*i;

int n=1;  for (int i=1;i<n;i++) {n=n*i;}

1.2, the idea of nesting is, f (n) =nf (n-1), when n equals 1 o'clock returns 1;

Private Static int factorial (int  n) {if(n==1)return 1; Else return n*factorial (n-1);}

1.3, the iterative process is relatively simple, do not want to say, in writing programs, can be written as iterations of the use of iterations, nesting relatively more consumption of computing resources.

Here's a brief look at the entire computational process of the iteration. The most important thing to write a nested algorithm is two points: program exit and program process. For example

The exit of the program refers to the value that the program's terminal program should return, such as the IF (n==1) return 1 in 1.2, which is the end of the program.

With the end of the program, it is necessary to design the process of the program, the process of nesting is not easy to write, very tolerant of mistakes. For example, there are two methods, very similar, the results differ greatly:

 private  static  void  show1 (int    i) { //  TODO Auto-generated method stub  if  (i < 6) t (I + 1 else   System.out.print (i  + "" 
     
Private Static void show2 (int  i) {        //  TODO Auto-generated method stub        if (i < 6)             + 1)        
       + " ");    }

Similarly to the I initial value is 1 o'clock, show1 print is the 6,show2 printed is 6 5 4 3 2 1.

Analyze these two programs, Show1 (1), Return Show1 (2) ... Finally, when Show1 (6), the program ends, printing 6. Show2 (1) is not the same, Show2 (1) of the export is also 6, however, in the execution of Show1 (1), the program has a part (System.out.print (...) ) was not executed because the program entered the next nested Show2 (2), saying that no executed statements were temporarily suspended. So that each loop is suspended for a piece of code until SHOW2 (6), the program finds the exit and starts to execute the nested procedure back. First print 6 after 5 then 4 ... Note that printing is not first 1 and then 2 again 3 ...

It can also be seen that exporting in nested is important for the program.

A nested computer algorithm can be understood as suspending the execution code until the exit is found and executing the code from the exit.

There is a classic example is the problem of seeking greatest common divisor, here is not much to say the process, the predecessors of the algorithm left. The mathematical process is that (a, b) of the greatest common divisor, and (B,A%B) the largest number of conventions equal, the algorithm is known for Euclid, named Euclidean algorithm. The code is not here.

Of course, the linear process can be optimized, here is not much to say.

If the linear structure is relatively simple, the tree structure is much more complex, but also more useful, can solve many of the daily problems of the century.

2. Tree-shaped recursion

The tree algorithm is much more complex and can be easily understood using nested methods.

A simple example is the Fibonacci number:

The most commonly used mathematical description is f (n) =f (n-1) +f (n-2). n=2 when 1,n=1 is 1.

Using iterative or recursive methods is easy to implement, and this is not a detailed implementation process. Simply put the nested tree in the picture:

The tree process is quite complicated, and it's just one of the simplest examples, and here's an interesting example of how nesting can solve problems more easily than iterations in solving complex problems:

There are 1 Americans, and he has a family of $1 (100 cents) coins. One day, he found me in the grocery store, to me, "Hey, man, do you have any change?" I replied, "What kind do you want, I have 50 cents, 25 cents, 10 cents, 5 cents and 1 cents." "So much," he exclaimed, "I want to change 1 cents, can you tell me how many different ways there are?" ”

Of course, for this problem, I will not answer him, I immediately called to the next door is busy to crack the U.S. military network Jack, "hi,jack,help me" I shouted, Jack is busy with his work, quiet half a minute, he shook his head, said the sentence "I know what You want to say,wait me one miniter "and sure enough, in less than a minute, Jack said a number 292. When I was still in the fog, the American who killed a lot of my brain cells shouted, "Unbelievable, how did you do it, I spent a week just to sort out all the situations". Are you smart enough to think of a solution?

Using recursion is not very difficult to achieve, just a little bit of brains.

The mathematical description is this: assuming that FK (N) describes all the results of n cents exchanged for k gold coins, then FK (n) can be considered recursively, without the result of one of the coins, and the result of the redemption of the remainder of one of the coins, written as: Fk (n) =fk-1 (n) +fk (n-a).

For example, the above example can be described as the end result is a 100 cent removal of a 50 cents from the remaining 50 cents to the total method of converting to 5 kinds of coins, plus 100 cents for the method of conversion to 4 coins (no 50 cents of this coin). Graphics can better illustrate the process, and smart you can do a graphic on the paper.

However, I would like to remind that it is not easy to write this mathematical process into computer code, here I do not give the Java language code, you can write it, let's talk about it for a moment.

Of course there is a challenge for everyone, including myself, is how to write this process in other ways, we all know that the recursive method is very resource-intensive, if you design a better algorithm, please share with me,[email protected]

Thank you!

Java process-nesting and iteration

Related Article

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.