In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "C language how to solve the rabbit birth problem," interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "how to solve the rabbit birth problem in C language"!
1. problem description
There is a pair of rabbits, born a pair of rabbits every month from the third month after birth.
After the rabbits grow to the third month, they give birth to a pair of rabbits every month. Assuming that all rabbits do not die, ask how many rabbits are there in each month for 30 months?
2. topic analysis
This is an interesting classical math problem. Let's draw a table to find out the pattern of rabbit counting.
Tip: Rabbits less than 1 month old are small rabbits, rabbits less than 1 month old are medium rabbits, and rabbits more than 3 months old are old rabbits.
As you can see, the total number of rabbits per month is 1, 1, 2, 3, 5, 8, 13…This is the Fibonacci sequence.
Summarize the law of series: that is, the number of rabbits in the first two months can be deduced from the number of rabbits in the third month.
3. algorithm design
The problem is a typical iterative loop, that is, a process of replacing the old value of a variable with a new value, and then recursing the new value of the variable from the old value.
This iteration depends on the following factors: initial value, iteration formula, iteration number. After analyzing the problem, the algorithm can be described as:
C language to describe the selection formula is fib=fibl+fib2.
Fib is the number of rabbits currently being evaluated.
fib1 is the number of rabbits in the previous month.
Fib2 stores the rabbit count from the previous two months, and then prepares for the next generation.
Fib2=fib1, fib1=fib, note the order of assignment, the number of generations is controlled by a cyclic variable, indicating the number of months sought.
4. code implementation
complete code
#include int main(){ long fib1 = 1; long fib2 = 1; long fib = 0; int i = 0; printf("dd", fib1, fib2); for (i = 3; i
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.