当前位置:网站首页>[Fibonacci series]
[Fibonacci series]
2022-06-11 02:36:00 【Cat star people who love Durian】
List of articles
1. Basic introduction
The Fibonacci sequence refers to a sequence that looks like this 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368
Tips : This sequence starts at number one 3 A start , Each of these terms is equal to the sum of the first two terms .
2. Recursive implementation
#include<stdio.h>
int Fib(int n)
{
if (n <= 2)
return 1;
else
return Fib(n - 1) + Fib(n - 2);
}
int main()
{
int n = 0;
scanf("%d", &n);
int ret = Fib(n);
printf("%d\n", ret);
return 0;
}
Running results

Time complexity :O(2^n)
Spatial complexity :O(n)
Tips : Space can be reused , Not cumulative ; Time is gone forever , Cumulative
3. Non recursive implementation
Iterative implementation
Tips : Time complexity :O(n) ; Spatial complexity O(1)
#include<stdio.h>
int Fib(int n)
{
int a = 1;
int b = 1;
int c = 1;
while (n > 2)
{
c = a + b;
a = b;
b = c;
n--;
}
return c;
}
int main()
{
int n = 0;
scanf("%d", &n);
int ret = Fib(n);
printf("%d\n", ret);
return 0;
}
Running results
Array implementation
Tips : Time complexity :O(n) ; Spatial complexity O(n)
#include<stdio.h>
#include<stdlib.h>
long long Fibonacci(unsigned int n)
{
if (n == 0)
return 0;
long long ret=0;
long long* fibArray = (long long*)malloc((n + 1) * sizeof(long long));
if (fibArray == NULL)
{
printf(" Space development failed ");
}
else
{
fibArray[1] = 1;
fibArray[2] = 1;
for (long long i = 3; i <= n; i++)
{
fibArray[i] = fibArray[i - 1] + fibArray[i - 2];
}
ret = fibArray[n];
free(fibArray);
fibArray = NULL;
}
return ret;
}
int main()
{
unsigned int n = 0;
scanf("%d", &n);
long long ret = Fibonacci(n);
printf("%lld\n", ret);
return 0;
}
The above is the whole content of this article , If there are mistakes in the article or something you don't understand , Communicate more with meow bloggers . Learn from each other and make progress . If this article helps you , Can give meow bloggers a concern , Your support is my biggest motivation .
边栏推荐
- 软件测试英语常见词汇
- Kotlin apply method
- Dynamically add attributes to objects
- STC8A8K64D4 EEPROM读写失败
- What is the relationship between precious metal silver and spot Silver
- Project - redis message queue + worker thread fetches user operation logs and stores them (2)
- Web watermark
- 20220610 星期五
- Jetpack compose box control
- Can annuity insurance financial products be compounded? What is the interest rate?
猜你喜欢

MySQL backup and recovery

Navicat Premium 15 工具自动被杀毒防护软件删除解决方法

Principle of everything for fast search

Byte beating | the first batch of written examination for game R & D post (question solution)

Jetpack compose scaffold and bottomappbar (bottom navigation)
![[parallel and distributed systems] cache learning](/img/79/de4da45aab54bb3bec240ac36e7978.png)
[parallel and distributed systems] cache learning

To view the data in redis, in addition to the command line and client, you have a third option

Metal organic framework materials (fe-mil-53, mg-mof-74, ti-kumof-1, fe-mil-100, fe-mil-101) supported on isoflurane / methotrexate / doxorubicin (DOX) / paclitaxel / ibuprofen / camptothecin

378. the k-th smallest element in an ordered matrix

Common vocabulary of software testing English
随机推荐
Add SQL formatter to vscode to format SQL
Unity serial port communication
Byte beating | the first batch of written examination for game R & D post (question solution)
PHP starts OpenSSL and reports OpenSSL support=> disabled (install ext/openssl)
Jetpack Compose Scaffold和BottomAppBar(底部导航)
Setting access to win10 shared folder without verification
Xampp is used under M1 chip, and the installation extension error
年金保險理財產品可以複利嗎?利率是多少?
Customized redistemplate in redis
Stc8a8k64d4 EEPROM read / write failure
Introduction for i-Teams
Use of CIN and cout
[AI weekly] AI and freeze electron microscopy reveal the structure of "atomic level" NPC; Tsinghua and Shangtang proposed the "SIM" method, which takes into account semantic alignment and spatial reso
Unity HTC and Pico are the same
When a logical deletion encounters a unique index, what are the problems and solutions?
10007. ISBN号码
Project - redis message queue + worker thread fetches user operation logs and stores them (2)
NFT insider 61:animoca brands holds US $1.5 billion of encrypted assets in 340 investments
Multilevel mesoporous organometallic framework material zif-8 loaded with lactic acid oxidase (LOD) / ferric oxide (Fe304) / doxorubicin / insulin /cas9 protein / metronidazole / emodin methyl ether
app 测试 常用 adb 命令集合


