1. Thinking
The rabbit problem is a figurative statement of the Fischer series, which is a question raised in its works by a mathematician named Fibonacci.
2. Description
The problem with its physical technique is: if a baby gives birth to a baby every month, the baby starts giving birth a baby a month later. At first there was only one baby, and there were two baby boys in one month, three baby boys in two months, and five baby boys in three months (the little baby boy was put into production)...
We express it in mathematical way, which is the following set of sequences:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89......
Note: The newborn baby will take one month to start production! And these rabbits are immortal! ! !
3. Rules
When we are inexplicably exposed to this problem, it is difficult to find the rules, but think about this problem according to the laws of sequences in mathematics, and wait for comparison? Avatar difference? Or something else? Since this is a question raised by mathematicians, there should be certain mathematical rules in it, right? What is the rule? If you carefully analyze the above set of sequences, you already have the answer. That's right, it is expressed in one sentence. Starting from the third term, the sum of the first two terms equals the third term.
Assuming that the value of the nth term is fn, the regularity of the sequence is expressed as follows using mathematical formulas:
4. Pseudocode
The so-called pseudo-code is not real code. It cannot be executed on the machine. It is just a meaningful symbol between natural language and programming language that expresses program logic. For the pseudocode of the rabbit problem, we use the recursive method of the above formula here, and 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 recursion concept described in the previous article, you can refer to the previous "Hanno Problem" for details. Compared to everyone, you will no longer be too unfamiliar with recursion. Then, based on the mathematical formulas we obtained above, deducing such recursion pseudocode will be very concise and clear. But, um, you may have guessed it, I want to say it. Have you found a problem that when our n value is too large, the program will be slower?
If you find out, it means that you have thought about this issue seriously, and you should also solve the doubts in your heart. If there is still no doubt that is resolved, let me solve everyone's doubts. Why is it slower? The reason is that when we calculate the nth term, we need to calculate the n-1 and n-2 terms again, and both terms have been calculated before. When we find the next number, we still have to calculate it on the side. Invisibly, we have done a lot of useless work.
So, do we have a good way to solve this problem? There is a answer. According to the above analysis, when we solve the nth term, the previous n-1 and n-2 terms have been solved. So, why don’t we save it? ? ? ?
Haha, did you suddenly realize it? Yes! We use space to exchange time here, which can greatly improve efficiency! I won't write pseudo-code here.
5. Code
OK, I've sold it all, just upload the code:
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 thinking question. If a rabbit does not give birth to one rabbit and multiple rabbits, how should we solve it? Of course, what we mean by giving birth to multiple is a fixed number. One rabbit will not give birth to more and one rabbit will not give birth to less, otherwise it will be impossible to solve it.
There is no code here. You can find the appropriate resources online and see how to solve them.
Summarize
The above is all the content of this article about the code analysis of Java language to solve rabbit problems. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out!