1. Perform cyclic operation for 1~n and
To complete this calculation, you can use the following loop steps to find out:
A. Set the initial value of the sum variable sum to 0.
b. Sum is calculated and value is Addend
C. When value is below N, repeat operation
D. Calculate the value of the Sum+value and deposit the value in sum
E. Add 1 for each value.
1 Public Static intSum (intN)2 {3 intsum =0;4 for(inti =0; I <= N; i++)5 {6sum = sum +i;7 }8 returnsum;9}
View Code
2. Fibonacci Sequence
Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 ....
Therefore, the calculation of the nth value can be calculated according to the following method:
A. Make f[0] = 0, f[1] = 1
B. Variable I starting from 2
C. Perform the operation of the operations repeatedly before I reach N
D. Make f[i] = F[i-2] + f[i-1]
E. Each time I value plus 1.
1 Public Static intFibonacci (intN)2 {3 if(n = =0)4 {5 return 0;6 }7 Else if(n = =1)8 {9 return 1;Ten } One Else A { - returnFibonacci (N-2) + Fibonacci (N-1); - } the}
View Code
3. To continue ....
Algorithm interpretation: the basic algorithm