Java language for rabbit problem code analysis, java language for Rabbit

Source: Internet
Author: User

Java language for rabbit problem code analysis, java language for Rabbit

1. Thinking

The rabbit problem is a visual representation of the February series. It is a question raised by a mathematician named ononacci in his book.

2. Description

The problem with its body surgery is: if one child is not given birth to a child every month, the child is also produced after one month. At first, there was only one free Sub-Account, two free sub-accounts in one month, and three free sub-accounts in two months, five free sub-accounts after three months (small sub-accounts are put into production )......

We use the mathematical expression to express the following series:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ......

Note: It takes one month for new users to participate in production! And these rabbits are not dead !!!

3. Rule

When we come into contact with this problem inexplicably, it is difficult to find the rule, but according to the law of the number of mathematical series to think about this problem, proportional? ? Or what else? Since this is a question raised by mathematicians, should there be a certain mathematical law in it? What is the rule? carefully analyze the above series and you already have an answer. Yes, it is expressed in one sentence. From the third item, the sum of the first two items is equal to the third item.

Assuming that the value of nth is fn, the regularity of the series is expressed using mathematical formulas as follows:

4. pseudocode

The so-called pseudo-code is not a real code. It cannot be executed on machines. It is just a meaningful symbol between a natural language and a programming language that expresses the logic of the program. For the pseudocode of the rabbit problem, we use the recursive method of the above formula here, we can have the following pseudocode:

Procedure FIB (N) [IF (N <0) PRINT ("input error"); IF (N = 0 or n = 1) RETURN (N ); else return (FIB (N-1) + FIB (N-2);]

According to the recursive concept described in the previous article, For details, refer to the previous "Tower of Hanoi Problem", which is no stranger to recursion. Based on the above mathematical formula, recursive pseudocode like this can be very concise and clear. However, you may have guessed it. Are you aware of a problem? When our n value is too large, the program will be slow?

If you find out, it means that you have carefully considered this problem and solved your doubts. If you still have questions, let me solve them. Why is it slow? The reason is that when we calculate the next n items, we need to calculate the N-1 and N-2 items again, and these two items have been computed before, when we try to find another number, we still need to calculate it while it is invisible, so we have done a lot of useless work.

So, is there a good way to solve this problem? The answer is yes. According to the above analysis, when we are solving the n-th item, the first N-1 and N-2 items have been solved, so, why don't we save it ????

Haha, are there any sudden surprises! Here we use space for time, which can greatly improve efficiency! I will not write pseudo code here.

5. Code

Okay, close the sub-sale, and run the Code directly:

public class Fibonacci {   public static void main(String[] args) {     int[] fib = new int[20];       fib[0] = 0;      fib[1] = 1;       for(int i = 2; i < fib.length; i++) {       fib[i] = fib[i-1] + fib[i-2];      }      for(int i = 0; i < fib.length; i++){        System.out.print(fib[i] + " ");      }     System.out.println();   } } 

6. Thinking

Here, we propose a question. If a rabbit does not have a rabbit or multiple rabbits, how can this problem be solved? Of course, we say that the birth of multiple animals is a fixed number. There will be no more rabbits than a rabbit. Otherwise, there will be no way to solve the problem.

No code is available here. You can find the appropriate resources on the Internet and see how to solve them.

Summary

The above is all the content of this article on the code analysis for solving rabbit problems in Java language. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message!

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.