1 Public classDigui {2 3 Public Static voidMain (string[] args) {4 //TODO auto-generated Method Stub5Fun (3);6 }7 8 Static voidFuninta) {9 inti;Ten System.out.print (a); One for(i = 0; i < A; i++) AFun (A-1); - } - the}
Output results: 3210102101021010
Execution process
So let's say:
Fun (3);//When initial invocation, a value equals 3
In order to be convenient to say, after calling the fun function, a value is equal to a few, called the number of recursion
Fun (3)//3 recursive start execution
Run printf ("%d", a); Output 3
Run for Loop, I=0,i<3,
Call Fun (3-1)
Fun (2)//2 recursive start execution
Run printf ("%d", a); Output 2
Run for Loop, I=0,I<2,
Call Fun (2-1)
Fun (1)//1 recursive start execution
Run printf ("%d", a); Output 1
Run for Loop, i=0,i<1,
Call Fun (1-1)
Fun (0)//0 recursive start execution
Run printf ("%d", a); Output 0
Run for Loop, i=0,i<0, not set
------------------------> To this point, output 3,2,1,0
After the recursion of number No. 0 is not established, return to the recursive 1th for loop for i++ processing,
Run for Loop, i=1,i<1, not set
After the recursion of number 1th is not established, return to the recursive 2nd for loop for i++ processing,
Run for Loop, I=1,I<2,
Call Fun (2-1)
Run again Fun (1)//1 number recursion
As can be seen from the above, the 1th recursive operation will output 1,0
------------------------> To this point, output 3,2,1,0,1,0
After the recursion of number 1th is not established, return to the recursive 2nd for loop for i++ processing,
Run for Loop, i=2,i<2, not set
After the recursion of number 2nd is not established, return to the recursive 3rd for loop for i++ processing,
Run for Loop, I=1,i<3,
Call Fun (3-1)
Run again Fun (2)//2 number recursion
As can be seen from the above, the 2nd recursive operation will output 2,1,0,1,0
------------------------> To this point, output 3,2,1,0,1,0,2,1,0,1,0
After the recursion of number 2nd is not established, return to the recursive 3rd for loop for i++ processing,
Run for Loop, I=2,i<3,
Call Fun (3-1)
Run again Fun (2)//2 number recursion
As can be seen from the above, the 2nd recursive operation will output 2,1,0,1,0
------------------------> To this point, output 3,2,1,0,1,0,2,1,0,1,0,2,1,0,1,0
After the recursion of number 2nd is not established, return to the recursive 3rd for loop for i++ processing,
Run for Loop, i=3,i<3, not set
At this point recursion ends
So the result of the final output is:
3 2 1 0 1 0 2 1 0 1 0 2 1 0 1 0
Further, change the function to:
1 Public classDigui {2 3 Static intm = 0;4 Static intn = 0;5 Public Static voidMain (string[] args) {6 //TODO auto-generated Method Stub7Fun (3);8 System.out.println ();9System.out.println ("n=" +n);TenSystem.out.println ("m=" +m); One } A - Static voidFuninta) { - inti; the System.out.print (a); - for(i = 0; i < A; i++){ -n++; -Fun (A-1); +m++; - } + } A at}
The output is:
3210102101021010
N=15
M=15
After the function executes the n++, it would have to execute m++, but suddenly into the stack, blocking, but the stack will certainly continue to follow the rest of the execution. The values of N and M must be the same
Recursive explanation (i)