【正文】
{j= g ( 3 ) 。 else m= f (n1)*n。 { int m。} void main( ) { int a , n , q=0 , j 。 for ( j=1。 } 遞歸調(diào)用 終止條件 P174例 根據(jù)習(xí)題課思路 void move ( char x , char y ) { printf (%c==%c\n, x , y ) 。 hanoi ( n1 , two , one , three ) 。 hanoi ( m , ’A’ , ’B’ , ’C’ )。 scanf (%d , amp。 else { hanoi ( n1 , one , three , two ) 。j++) q+=p(a,j)。a,amp。else m=p(a,n1)*10+a。 } void main( ) { printf (“ 4!=%d\n“ ,f (4) )。 } 直接調(diào)用自己 間接調(diào)用自己 例 10. 計(jì)算 4! 4!————————————— 24 ↓ 3!*4———————————— 6*4=24 ↓ 2!*3———————————— 2*3=6 ↓ 1!*2———————————— 1*2=2 ↓ 1 n! ? (n1)!*n 遞歸公式 int f ( int n ) { int m 。為了防止遞歸調(diào)用無終止地進(jìn)行,必須加條件判斷,滿足某種條件后就不再作遞歸調(diào)用,然后逐層返回。 z=f(y)。 printf(”\n”)。k=j 。j8。 } int f ( int m ) void main ( ) {int i , fac 。 …… } P2() { …… } P2() { …… } P2() { …… } P1() { …… P2 …… } 1 C 1 1 C C 1 2 1 C C C 1 3 3 1 C C C C 1 4 6 4 1 …… 1 5 10 10 5 1 0 0 0 1 1 1 1 2 2 2 0 2 1 3 2 3 3 3 0 3 C r n = n! r!(nr)! 例 8. 打印楊輝三角形( 8行) int r ( n , r ) int n , r 。 printf(”p3!!!\n”); } 1 2 3 4 5