. 1 #include <stdio.h> 2 . 3 void A ( void ); // function declarations . 4 void B ( void ); . 5 void C ( void ); . 6 . 7 void A ( void ) . 8 { . 9 the printf ( " AAAA \ n- " ); // ② complete this step, then jumps to b () function 10 b (); 11 printf ( " 1111 \ the n- " ); // ⑥ executing the a () statement after the rest back to the main function 12 } 13 14 void B ( void ) 15 { 16 the printf ( " bbbb \ n- " ); // ③ execution jumps to the completion of this step C () function . 17 C (); 18 is the printf ( " 2222 \ n- " ); // ⑤ after execution B () after the remaining statements back to a () . 19 } 20 is 21 is void C ( void ) 22 is { 23 is the printf ( " CCCC \ n- " ); 24 the printf ( " 3333 \ n- " ); //④ After completion of execution C () Back B () function to execute the remaining statements 25 } 26 is 27 int main ( void ) // ① from the main program start function 28 { 29 A (); 30 31 is return 0 ; 32 }
Output:
aaaa bbbb cccc 3333 2222 1111
Example 3. Recursive
1 #include<stdio.h> 2 3 void f(int n) 4 { 5 6 if(n==1) 7 printf("哈哈!\n"); 8 else 9 { 10 printf("2\n"); 11 f(n-1); 12 } 13 } 14 15 int main(void) 16 { 17 f(5); 18 return 0; 19 } 20
Output:
2 2 2 2 ha ha!
4. Why can call their own own function
A calls function A function and the B function A function call in the computer appears to be no different (see function calls C_ _ notes function), but with our daily way of thinking to understand weird has been.
The three conditions that must be met recursive
① recursive must have a clear termination condition
② the size of the data processing function must be decremented
③ this conversion must be solvable
6. Comparison of recursive cycles and
① recursive
readily appreciated
slow
storage space
② cycle
incomprehensible
speed
memory of small
cases:
1 // factorial implemented cycle 2 . 3 #include <stdio.h> . 4 . 5 int main ( void ) . 6 { . 7 int n-; . 8 int I; . 9 int SUM = 1 ; 10 Scanf ( " % D " , & n-) ; . 11 12 is for (I = . 1 ; I <= n-; ++ I) 13 is { 14 SUM = I * SUM; 15 } 16 . 17 the printf ( "%d\n",sum); 18 19 return 0; 20 }
1 // factorial using recursive 2 . 3 #include <stdio.h> . 4 . 5 int F ( int n-) . 6 { . 7 IF ( 1 == n-) . 8 return 1 ; . 9 the else 10 return n-* F (N- 1 ) ; . 11 } 12 is 13 is int main ( void ) 14 { 15 int n-; 16 Scanf ( " % D " , & n-); . 17 18 is the printf ( " factorial is% d% d " , n-, F (n-)); . 19 20 is return 0 ; 21 is }
7. A recursive application
① and forest trees is recursively defined
② lot number and the algorithm map is implemented in a recursive
③ many mathematical formula is recursively defined (Example: Fibonacci sequence)
8.1 + 2 + 3 + ... + 100 implemented recursively
1 #include<stdio.h> 2 3 int max(int i) 4 { 5 if(1==i) 6 return 1; 7 else 8 return max(i-1)+i; 9 } 10 11 int main(void) 12 { 13 int i=100; 14 15 printf("%d",max(i)); 16 17 return 0; 18 }
9. Tower of Hanoi program
. 1 #include <stdio.h> 2 . 3 void hannuota ( int n, char A, char B, char C) . 4 { . 5 / * if n is equal to 1 . 6 directly on the A plate is moved to the C . 7 or 8 to the A of the n-1 to B C is moved by means of a plate . 9 directly on the n-th plate a moves to C 10 n-1 on a plate will move to B means a C . 11 * / 12 is 13 is IF ( . 1 == n ) 14 the printf ( " the number% d% c from plate to move% c \ n- " , n-, a, C); 15 the else 16 { . 17 hannuota (N- . 1 , A, C, B); 18 is the printf ( " The number% d% c from plate to move% c \ n- " , n-, A, C); . 19 hannuota (N- . 1 , B, A, C); 20 is } 21 is } 22 is 23 is int main ( void ) 24 { 25 int n-; 26 is char CHl = ' A ' ; 27 char CH2 = ' B ' ; 28 char CH3 = ' C'; 29 scanf("%d",&n); 30 31 hannuota(n,ch1,ch2,ch3); 32 33 return 0; 34 }