当前位置:网站首页>[C language] Fibonacci sequence and frog jumping steps (the most detailed elementary frog jumping steps)
[C language] Fibonacci sequence and frog jumping steps (the most detailed elementary frog jumping steps)
2022-06-29 01:36:00 【YeMing_ LCH】
Catalog
- The solution of recursive Fibonacci number
- Iterative solution to Fibonacci number
- A detailed explanation of the frog jumping on the steps
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... This sequence starts at number one 3 A start , Each of these terms is equal to the sum of the first two terms . This series is also called golden section series .
Raise questions : How to use C Language solution No n A Fibonacci number ?
programme 1: recursive
Ideas : When n <= 2 when , Direct output 1;
When n > 2 when , Use the formula f(n) = f(n-1) + f(n-2) Realize recursive operation .
#define _CRT_SECURE_NO_WARNINGS 1 #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", ret); return 0; }shortcoming : A great deal of repetition has been done ! When n When a large , The operation time is greatly prolonged , The efficiency is greatly reduced !
programme 2: iteration
Ideas : From to later , Shilling a = 1,b = 1,c = 1.
When n <= 2 when , Direct output c;
When n > 2 when ,c = a + b, So we get the third number , Then make a = b, b = c, Count again c = a + b, So we get the fourth number ...
int fib(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); printf("%d", fib(n)); return 0; }advantage : Avoid a lot of repeated calculations , Greatly improve the operation efficiency !
The problem of frog jumping on the steps
The rules : Frogs can jump one or two steps at a time
problem : Frog jumps n How many options are there for each step ?
analysis 1:1 A stair - There is a plan ;
analysis 2:2 A stair - There are two options ;
analysis 3:3 A stair - The first step is to jump 1 individual , And then there were 2 individual ; The first step is to jump 2 individual , And then there were 1 individual ; Number of schemes = jump 2 Number of projects + jump 1 Number of projects ;
analysis 4:4 A stair - The first step is to jump 1 individual , And then there were 3 individual ; The first step is to jump 2 individual , And then there were 2 individual ; Number of schemes = jump 3 Number of projects + jump 2 Number of projects ;
analysis 5:5 A stair - The first step is to jump 1 individual , And then there were 4 individual ; The first step is to jump 2 individual , And then there were 3 individual ; Number of schemes = jump 4 Number of projects + jump 3 Number of projects ;
...
analysis n:n A stair - Number of schemes = jump n-1 Number of projects + jump n-2 Number of projects .
summary : This is a Fibonacci sequence problem , There are also two solutions : Recursion and iteration
#include <stdio.h> // recursive int solve1(int n) { switch (n) { case 1: return 1; case 2: return 2; default: return solve1(n - 1) + solve1(n - 2); } } // iteration int solve2(int n) { int a = 1; int b = 2; int c = 0; if (n == 1) { return 1; } if (n == 2) { return 2; } while (n > 2) { c = a + b; a = b; b = c; n--; } return c; } int main() { int n = 0; scanf("%d", &n); printf(" Recursive results :%d\n", solve1(n)); printf(" Iteration results :%d\n", solve2(n)); return 0; }tips: The frog jumping steps is a little different from the Fibonacci sequence when n = 2 When the results are different , So there are differences in some codes , But the big idea remains the same .
边栏推荐
- Installation de la base de données Oracle dans docker
- Statistical learning method (2/22) perceptron
- Analysis of advantages and disadvantages of environment encryption and transparent encryption
- C语言课程设计------食品仓库管理系统
- In simple terms, server intrusion prevention
- Similarities and differences between SRAM and DRAM
- Noip2006-2018 improvement group preliminary test questions improvement procedure questions csp-s 2019 2020 preliminary test questions improvement procedure questions
- [proteus simulation] 4x4 matrix keyboard interrupt mode scanning + nixie tube display
- SAP ui5 beginner tutorial 24 - how to use OData data model
- 7-27 bubble sorting (the k-th time)
猜你喜欢

What kind of life is a tester with a monthly salary of over 10000?

IPFs Brief

Battle drag method 1: moderately optimistic and build self-confidence (2)

4276. good at C

Latest version of CorelDRAW technical suite2022

Day 8 script and audio
![[image detection] recognition of the front and back of a coin based on texture features with matlab code attached](/img/61/1fb15e9defa1fc471c4d2d34cc1ed4.jpg)
[image detection] recognition of the front and back of a coin based on texture features with matlab code attached

SAP ui5 beginner tutorial 24 - how to use OData data model
![[js practice every m days] JS export object analysis based on libcef application (steam)](/img/ab/c242f42af3a7db7ce522622092bb6c.jpg)
[js practice every m days] JS export object analysis based on libcef application (steam)

Edrawmax mind map, edrawmax organization chart
随机推荐
Streaming media cluster application and configuration: how to deploy multiple easycvr on one server?
IPFS简述
一种全面屏手势适配方案
802.1x协议简述
The function of Schottky diode in preventing reverse connection of power supply
Business system anti-virus
SRAM和DRAM之间的异同
[image processing] image curve adjustment system based on MATLAB
PHP hospital network reservation management system source code, hospital consultation reservation registration OA system (commercial or graduation design)
Typescript (5) class, inheritance, polymorphism
XML and other file contents in idea cannot be highlighted, and the file becomes gray
Day 7 scripts and special effects
Interviewer: with the for loop, why do you need foreach??
Stm32l4xx serial port log configuration analysis
2022年启牛商学院证券账户开户安全的嘛?
Battle drag method 1: moderately optimistic and build self-confidence (2)
如何进行数据库选型
TypeScript(5)类、继承、多态
After easycvr creates a new user, the video access page cannot be clicked. Fix the problem
Sword finger offer 16 Integer power of numeric value