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 to solve the Rabbit problem with java

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

Share

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

How to use java to solve the rabbit problem, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.

Topic: classical question: there is a pair of rabbits who give birth to a pair of rabbits every month from the third month after birth, and the little rabbits grow up to give birth to another pair of rabbits every month after the third month. If the rabbits do not die, what is the total number of rabbits each month?

Answer:

Analysis: starting from the first pair of rabbits, one pair of rabbits in the first month, because "there is another pair of rabbits every month after growing to the third month", so the second month is also a pair of rabbits, and the third month can give birth to a pair of rabbits, so the total number is 2 pairs. At this time, one of them is an old rabbit who can give birth to a pair of rabbits every month, and the other is a pair of new rabbits. It can only give birth to a pair of rabbits a month in the third month. In that case, the total number of rabbits in the 4th month is 3 pairs. By analogy, the total number of rabbit pairs from the first month to the 12th month is as follows: 1magy 1pair2 pairs3, 5, 8, 13, 21, 34, 5, 89144.

This is a typical Fibonacci sequence.

Since each pair of new rabbits can only give birth to a new pair of rabbits every other month, the total number of rabbit pairs each month is the sum of the rabbit logarithms of the previous two months.

(method 1) it is basically realized by java programming, and the program design is as follows:

The code is implemented as follows:

Public class RabbitDemo {

Public static void main (String [] args) {

Int amount; / / the total number of rabbits per month

Int nAmount = 1; / / number of rabbits in the first month

Int oAmount = 1; / / number of rabbits in the second month

For (int iTunes 1)

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: 0

*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