Algorithm for solving rabbit numbers using Fibonacci sequence (Java) __ algorithm

Source: Internet
Author: User

Today saw an algorithm problem, calculate half a day did not calculate out, later looked at the data, originally this is a Fibonacci series of questions, the title is: A pair of rabbits, from the 3rd month after the birth of a pair of rabbits every month, the rabbit long to the third month after the birth of a pair of rabbits, if the rabbit is not dead, What is the total number of rabbits per month?

I started the idea is to first the Rabbit series out every month F1 = 1; F2 = 1; F3 = f2+1=2; F4 = f3+1=3; f5 = f4+1+1=5; f6 = f5+1+1+1=8; F7 = f6+1+1+1+1+1=13 ... (Keep going round)

So the rules are coming.
1.F3 = F1+F2;
2.F4=F2+F3;
3.f5 =f3+f4;
......

From the third, each item equals the sum of the first two;
To achieve the addition of two numbers, and then the first two number of swaps, and then I wrote a piece of code
public class suanfa1{
public static void Main (String [] args) {
int f1 = 1;
int f2=1;
int C; Set an intermediate variable
int m=12;
for (int i =3;i<=m;i++) {
c = F2;
F2= F1+f2;
F1=c; Swap the value of the A.B
System.out.println ("+i+" the number of rabbits in the month is: "+f2+");
}
}
}

The results of the operation are as follows:

The number of rabbits in the 3rd month is: 2 to the 4th month of the Rabbit number: 3 for the 5th month of the rabbit number is: 5 to the 6th months of the rabbit number
: 8 to the 7th of the number of rabbits:
13 to the
8th of the number of rabbits is: 21 to
The number of rabbits in the 9th month is: 34 to the
10th month of the rabbit number: 55 for the
11th month of the rabbit number is: 89 to
12th months of rabbit number: 144 pairs

Then my roommate looked at the code and said that the code could be simpler, and then he used the following algorithm to turn the operation of the loop body into just two, and his algorithm was this:

public class suanfa1{public
static void  main (String [] args) {
            int now =1;    
            int last =0;
            int m=12;   set the time for one year for
            (int i =1;i<=m;i++) {
                System.out.println ("+i+") the number of rabbits for the month is: "+now+");   
                Now+=last;  
                Last =now-last}}}
    

The results of the operation are as follows:

The number of rabbits in the 1th month is: 1 to the 2nd month of the rabbit number: 1 for the 3rd month of the rabbit number is: 2 to the 4th months of the rabbit number
: 3 to the 5th of the number of rabbits:
5 to the
6th of the number of rabbits is: 8 to
The number of rabbits in the 7th month is: 13 to the 8th month of the rabbit number: 21 for the 9th month of the rabbit number is
: 34 to the
10th months of the rabbit number: 55 to the 11th of the number of rabbits
is: 89 to the
12th month of the number of rabbits as : 144 to

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.