catalogue
one point one What is the Fibonacci sequence
one point two The law of Fibonacci sequence
2. Realize Fibonacci sequence with code thinking
2.1 are you sure which Fibonacci sequence to find
2.2 how to use Fibonacci sequence law
two point three How to realize Fibonacci sequence by loop
two point four How to enter the cycle
two point five How to jump out of a loop
1. Know Fibonacci sequence
1.1 what is the Fibonacci sequence
Generally speaking, it refers to such a sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34
1.2 what are the rules of Fibonacci sequence
Generally speaking, the rule of Fibonacci sequence is: the sum of the first two numbers equals the third number
For example: 1 + 1 = 2, 1 + 2 = 3
2. Realize Fibonacci sequence with code thinking
2.1 determine which Fibonacci number to find
First, you need to define a variable to store the Fibonacci sequence received from the keyboard
#include <stdio.h> int main() { int n=0; scanf("%d",&n);//Use n to receive the value of the Fibonacci sequence you want to query return 0; }
2.2 how to use rules to realize Fibonacci sequence
From the above, we know that the Fibonacci sequence is the sum of the first two numbers equal to the third number (and so on), so we need to To create three variables to represent the first two numbers and the third number, and assign the latter number to the previous number The numbers go back one by one
#include <stdio.h> int main() { int n=0; int a=1; int b=1;//Because the first and second Fibonacci series do not need to be calculated, it can be given int c=0; scanf("%d",&n);//Use n to receive the value of the Fibonacci sequence you want to query c=a+b;//Start with the third number a=b; b=c;//Add backward in turn by assigning values to each other }
2.3 how to realize Fibonacci sequence by using loop
From the code of 2.2, we have written out the overall running rules, but if we want to query the Fibonacci number later, we can These rules need to be repeated, so we need to use the loop to calculate which result we want
#include <stdio.h> int main() { int n=0; int a=1; int b=1;//Because the first and second Fibonacci series do not need to be calculated, it can be given int c=0; scanf("%d",&n);//Use n to receive the value of the Fibonacci sequence you want to query while()//So far, how to enter the cycle is unknown { c=a+b;//Start with the third number a=b; b=c;//Add backward in turn by assigning values to each other } }
2.4 how to enter the cycle
From the above code, we move the number back and calculate it from the third number, so
We enter the loop from three to calculate (because the first number and the second number are given)
#include <stdio.h> int main() { int n=0; int a=1; int b=1; int c=0; scanf("%d",&n); while(n>2)//n is calculated from the third { c=a+b; a=b; b=c; } }
two point five How to jump out of a loop
Take chestnuts: for example, we have to wash 10 clothes. How can we calculate that we have completed the task? Of course, the clothes are light
Similarly, when do we jump out of the loop? Of course, when the loop executes light. When n is given again
In fact, it has been determined after recycling several times. After recycling once, we will throw away the recycled (minus one)
Until it can't cycle (that is, when n is 2, it doesn't need to be calculated, that is, it doesn't need to cycle)
#include <stdio.h> int main() { int n=0; int a=1; int b=1; int c=0; scanf("%d",&n); while(n>2) { c=a+b; a=b; b=c; n--;//Until no cycle is required } printf("%d",c); }
Last step
What if we just want the first or second Fibonacci number
At this time, there is no need to enter the loop to output c, and the value will be wrong, so directly changing c to one will not affect the operation and the result
#include <stdio.h> int main() { int n=0; int a=1; int b=1; int c=1; scanf("%d",&n); while(n>2) { c=a+b; a=b; b=c; n--;//Until no cycle is required } printf("%d",c); }
For a superficial understanding of Fibonacci sequence, I hope all leaders can guide me