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 realize Fibonacci sequence in C language

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the relevant knowledge of how C language realizes Fibonacci series, the content is detailed and easy to understand, the operation is simple and fast, and it has certain reference value. I believe you will gain something after reading this article on how to realize Fibonacci series in C language. Let's take a look.

Recursive Fibonacci sequence of C language data structure

First of all, recursion provides a simple definition of recursion depth. If Fibonacci () is called, Fibonacci (n) should return 1 when n is 1 or 2, and Fibonacci (NMel 1) + Fibonacci (NMel 2) for other values.

Long Fibonacci (n) {if (n > 2) return Fibonacci (nmai 1) + Fibonacci (nmai 2); else return 1;}

Then there is the question of the total number of rabbits.

There is a pair of rabbits that 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 after the third month. If the rabbits do not die, what is the number of rabbits per month?

When you think about this problem, if you do a simple calculation, you will find that the logarithm of the rabbit each month is the Fibonacci series.

First month: 1 pair

Second month: 1 pair

Third month: 2 pairs

Fourth month: 3 pairs:

Fifth month: 5 pairs:

Sixth month: 8 pairs

……

When I was working on this problem, I thought the idea was very simple, that is, from the third month, when I asked for the number of rabbits each month, I just had to add up the total number of the first two months of this month.

This is my previous code, using F1 and f2 to represent the month. :

# includeint main () {int F1 dint f2; int month,ct; printf ("Please enter month:"); scanf ("% d", & month); if (month 2) {F1 = f2 = 1; ct = 0; while (ct)

< month -2){ f1 = f1+f2; ct += 1; f2 = f1+f2; ct += 1; } if (month %2 == 0){ printf("第 %d 个月的兔子对数为:%d.\n",month,f2); } if (month %2 == 1){ printf("第 %d 个月的兔子对数为:%d.\n",month,f1); } } return 0;} 其实这个代码离递归就差一步,很接近了。但是我当时完全没有想到。 这是我重新修改之后的代码: #includelong Fibonacci(n){ if (n >

2) return Fibonacci (nMel 1) + Fibonacci (nMel 2); else return 1;} int main () {long num; int month; printf ("Please enter month:"); scanf ("% d", & month); num = Fibonacci (month); printf ("this month's rabbit logarithm is% d.\ n", num); return 0 } this is the end of the article on "how to realize Fibonacci sequence in C language". Thank you for reading! I believe you all have a certain understanding of "how to realize Fibonacci series in C language". If you want to learn more, you are welcome to follow the industry information channel.

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