The sum of the First n digits of Fibonacci and the n digits of Fibonacci
Public class Fei_bo_na_qi {
Public static void main (String [] args) {// main method, program entry
Int I = 10; // declare an int type variable I, and assign a value of 10
Int a = 0; // declare an int data type variable
For (int j = I; j> = 1; -- j) {// for Loop: j = I = 10, j> = 1
A + = m1 (j); // call the m1 method and assign the value of m1 to
}
System. out. println (a); // output the value of.
}
Public static int m1 (int n) {// declare the m1 method, the form parameter is int n, bringing n = 10
If (n = 1) {// when n = 1, the return value is 0.
Return 0;
}
If (n = 2) {// when n2, the return value is 1
Return 1;
} Else {// when n is neither 1 nor 2, execute the following statement
Return m1 (n-1) + m1 (n-2); // After n = 10 is brought in, return m1 (9) + m1 (8), -- j = 9,9> = 1; after bringing in, m1 (8) + m1 (7) is returned, -- j = 8 is brought in, and so on. When n = 3, m1 (2) is returned) + m1 (1) = 0 + 1 = 1, m1 (3) = 1, m1 (4), m1 (4) = m1 (3) + m1 (4-2), m1 (3) = 1, bring m1 (4-2) into the run once, every time m1 (n-1) runs once, to m1 (n-2) run again; Also to n = 10 into, return m1 (9) + m1 (8), m1 (8), -- j = 7, 7> = 1; after the return m1 (6) + m1 (5), every run m1 (n-2), m1 (n-2) to run again. Finally, the m1 value is obtained, assigned to a, and finally output.
Fibonacci, the puzzle that plagued me for a long time, was finally solved.