The usage analysis of Java recursive algorithm _java

Source: Internet
Author: User

Recursive algorithm is a kind of algorithm that calls itself directly or indirectly. In computer programming, recursive algorithm is very effective to solve a large class of problems, it often makes the description of the algorithm concise and easy to understand.

Question 1: The rules for a number of columns are as follows: 1, 1, 2, 3, 5, 8, 13, 21, 34, what is the 30th digit number? Using recursion to implement

Copy Code code as follows:

public class Fibonaccisequence {
public static void Main (string[] args) {
System.out.println (Fribonacci (9));

}
public static int Fribonacci (int n) {
if (n<=2)
return 1;
Else
Return Fribonacci (n-1) +fribonacci (n-2);

}
}

Question 2: Hanoi tower problem

Hanoi (also known as Heneta) is an ancient legend in India.

The Blama of the epoch-making gods (like the god of Pangu in China) left three sticks of diamond in a temple, the first one was covered with 64 pieces of gold, the largest one at the bottom, and the other a little, stacked up, and the monks in the temple tirelessly moved them from the rod to the other. The provision can be used in the middle of a bar as a help, but only one at a time, and the big can not be placed on the small. The results were very scary (the number of moves to the wafer) 18446744073709551615, and the monks could not have made the gold move even if they had spent their entire lives.

Requirement: Enter a positive integer n indicating that there are n platters on the first pillar. The output action sequence, formatted as "Move t from X to Y". Each action line means that the disc numbered T on the X pillar is moved to the column Y. The column number is a,b,c, you need to use the least operation to transfer all the plates from a pillar to the C pillar.

Copy Code code as follows:

public class Hanio {
public static void Main (string[] args) {
int i=3;
char a = ' a ', b= ' B ', c= ' C ';
Hanio (I,A,B,C);
}
public static void Hanio (int n,char A,char B,char c) {
if (n==1)
System.out.println ("Move" +n+ "plate" from "+a+" to "+c");
else{
Hanio (N-1,A,C,B)//To n-1 a plate from a to C with the help of B
System.out.println ("Move" +n+ "plate" from "+a+" to "+c");/immediately followed by moving N directly
Hanio (N-1,b,a,c), and then the n-1 plate on B to the C
}
}
}

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.