C++ Tutorial/Function/function recursion — различия между версиями

Материал из C\C++ эксперт
Перейти к: навигация, поиск
м (1 версия: Импорт контента...)
 
(нет различий)

Текущая версия на 10:29, 25 мая 2010

Demonstrate recursion

#include <iostream> 
using namespace std; 
 
int f(int n); 
 
int main() 
{ 
  // use recursive version 
  cout << "4 factorial is " << f(4); 
 
  return 0; 
} 
 
int f(int n)  
{ 
  int answer; 
 
  if(n==1)  
      return(1); 
  
  answer = f(n-1)*n; 
  return(answer); 
}
4 factorial is 24

Print a string backwards using recursion

#include <iostream> 
using namespace std; 
 
void f(char *s); 
 
int main() 
{ 
  char str[] = "this is a test"; 
 
  f(str); 
 
  return 0; 
} 
 
void f(char *s) 
{ 
  if(*s)  
    f(s+1);  
  else 
    return; 
 
  cout << *s; 
}
tset a si siht

Recursive factorial function

#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
unsigned long factorial( unsigned long ); // function prototype
int main()
{
   for ( int counter = 0; counter <= 50; counter++ )
      cout << setw( 2 ) << counter << "! = " << factorial( counter ) << endl;
   return 0;
}
unsigned long factorial( unsigned long number )
{
   if ( number <= 1 )
      return 1;
   else
      return number * factorial( number - 1 );
}
0! = 1
 1! = 1
 2! = 2
 3! = 6
 4! = 24
 5! = 120
 6! = 720
 7! = 5040
 8! = 40320
 9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600
13! = 1932053504
14! = 1278945280
15! = 2004310016
16! = 2004189184
17! = 4006445056
18! = 3396534272
19! = 109641728
20! = 2192834560
21! = 3099852800
22! = 3772252160
23! = 862453760
24! = 3519021056
25! = 2076180480
26! = 2441084928
27! = 1484783616
28! = 2919235584
29! = 3053453312
30! = 1409286144
31! = 738197504
32! = 2147483648
33! = 2147483648
34! = 0
35! = 0
36! = 0
37! = 0
38! = 0
39! = 0
40! = 0
41! = 0
42! = 0
43! = 0
44! = 0
45! = 0
46! = 0
47! = 0
48! = 0
49! = 0
50! = 0

The iterative factorial method.

#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
unsigned long factorial( unsigned long );
int main()
{
   for ( int counter = 0; counter <= 20; counter++ )
      cout << setw( 2 ) << counter << "! = " << factorial( counter ) << endl;
   return 0;
}
unsigned long factorial( unsigned long number )
{
   unsigned long result = 1;
   for ( unsigned long i = number; i >= 1; i-- )
      result *= i;
   return result;
}
0! = 1
 1! = 1
 2! = 2
 3! = 6
 4! = 24
 5! = 120
 6! = 720
 7! = 5040
 8! = 40320
 9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600
13! = 1932053504
14! = 1278945280
15! = 2004310016
16! = 2004189184
17! = 4006445056
18! = 3396534272
19! = 109641728
20! = 2192834560

The recursive fibonacci function.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
unsigned long fibonacci( unsigned long );
int main()
{
   cout << "fibonacci( 20 ) = " << fibonacci( 20 ) << endl;
   cout << "fibonacci( 30 ) = " << fibonacci( 30 ) << endl;
   cout << "fibonacci( 35 ) = " << fibonacci( 35 ) << endl;
   return 0;
} 
unsigned long fibonacci( unsigned long number )
{
   if ( ( number == 0 ) || ( number == 1 ) )
      return number;
   else 
      return fibonacci( number - 1 ) + fibonacci( number - 2 );
}
fibonacci( 20 ) = 6765
fibonacci( 30 ) = 832040
fibonacci( 35 ) = 9227465