The title is from the 4th question of "garlic guest".
Before solving the algorithm problem, we must first write the corresponding mathematical expression, which is used to describe the algorithm.
Mathematical description
According to the "Mathematical description", write the code as follows:
#include <stdio.h>intMain () {intN =0 ; scanf ("%d", &N); intI, fn1 =1, fn2 =0, FN; Switch(N) { Case 0: printf ("0"); Break; Case 1: printf ("1"); Break; default: for(i =2; I <= N; i++) {fn= Fn1 +fn2; FN2=fn1; FN1=fn; } printf ("%d", FN); } return 0;}
Happy to finish.
Reflection, the time complexity of the algorithm is O (N), that is, the execution time of the algorithm depends on the number of items in the sequence. If you can push the "nth Xiang Feipo formula" and bring it directly into the equation, then the time complexity will evolve to O (1).
Simple Fibonacci--garlic (4)