Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Java solve the classic rabbit problem?

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to solve the classic rabbit problem with Java". In daily operation, I believe many people have doubts about how to solve the classical rabbit problem with Java. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to solve the classic rabbit problem by Java". Next, please follow the editor to study!

I. introduction of the topic

Topic content:

Now there is a pair of little rabbits. It takes them three months to grow into big rabbits, and at the same time they will give birth to a pair of little rabbits. Assuming that none of the rabbits will die, how many rabbits will there be in three years?

Topic analysis:

When many children see such a problem, they may have begun to get dizzy. Don't worry, let's analyze the problem slowly and find out the law of the problem.

In the first month, 1 0 01 pairs of rabbits 2 rabbits

In the second month, 0 01 pairs of rabbits, 2 rabbits.

In the third month, 1 0 1 2 pairs of 4 rabbits

4 months 1 1 1 3 pairs of rabbits 6 rabbits

The fifth month 2 1 2 5 pairs of rabbits 10 rabbits

In the sixth month, 3 2 3 8 pairs of rabbits 16 rabbits

In the seventh month, 5 3 5 13 pairs of rabbits 26 rabbits

8 months 8 5 8 21 pairs of rabbits 42 rabbits

……

Seeing this, I wonder if the friends have found the pattern inside.

From the third month, the total logarithm of rabbits is the sum of the previous two months.

March January quantity + February quantity 1x 1 2

April February + March quantity 1-2-3

Quantity in May March + quantity in April 2-3-5

We can get such a mathematical formula sum = n (xmurl) + n (xmuri 2) [x > 2]

Second, code display

The requirement of the topic is that there will be many rabbits after three years. Therefore, first declare a 36-bit array for traversal.

Because there are only two in the first month and the second month, so add the judgment, if it is the first month or the second month, then the value of the corresponding position is 1. 5%.

Continue to judge after the third month, begin the accumulation of the first two months. The specific code is as follows:

Public class Demo01 {/ * Classical Rabbit problem * Recursive Fibonacci sequence * * / public static void main (String [] args) {int i; int [] arr=new int [36]; for

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 298

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report