📜  递归函数

📅  最后修改于: 2021-04-27 05:52:30             🧑  作者: Mango

T N =(N-1)+ T N-1 [T 1 = 0，即最后一个人已经与每个人握手]

##### C++
``````#include
using namespace std;

// Assuming n-th disk is bottom disk (count down)
void tower(int n, char sourcePole,
char destinationPole, char auxiliaryPole)
{

// Base case (termination condition)
if(0 == n)
return;

// Move first n-1 disks from source pole
// to auxiliary pole using destination as
// temporary pole
tower(n - 1, sourcePole, auxiliaryPole,
destinationPole);

// Move the remaining disk from source
// pole to destination pole
cout << "Move the disk "<< n << " from " <<
sourcePole <<" to "<< destinationPole << endl;

// Move the n-1 disks from auxiliary (now source)
// pole to destination pole using source pole as
// temporary (auxiliary) pole
tower(n - 1, auxiliaryPole, destinationPole,
sourcePole);
}

// Driver code
int main()
{
tower(3, 'S', 'D', 'A');

return 0;
}

// This code is contributed by SHUBHAMSINGH10``````

##### C
``````#include

// Assuming n-th disk is bottom disk (count down)
void tower(int n, char sourcePole, char destinationPole, char auxiliaryPole)
{
// Base case (termination condition)
if(0 == n)
return;

// Move first n-1 disks from source pole
// to auxiliary pole using destination as
// temporary pole
tower(n-1, sourcePole, auxiliaryPole,
destinationPole);

// Move the remaining disk from source
// pole to destination pole
printf("Move the disk %d from %c to %c\n",
n,sourcePole, destinationPole);

// Move the n-1 disks from auxiliary (now source)
// pole to destination pole using source pole as
// temporary (auxiliary) pole
tower(n-1, auxiliaryPole, destinationPole,
sourcePole);
}

int main()
{
tower(3, 'S', 'D', 'A');

return 0;
}``````

##### Java
``````// Assuming n-th disk is
// bottom disk (count down)
class GFG {

static void tower(int n, char sourcePole,
char destinationPole, char auxiliaryPole)
{
// Base case (termination condition)
if (0 == n)
return;

// Move first n-1 disks from source pole
// to auxiliary pole using destination as
// temporary pole
tower(n - 1, sourcePole, auxiliaryPole,
destinationPole);

// Move the remaining disk from source
// pole to destination pole
System.out.printf("Move the disk %d from %c to %c\n",
n, sourcePole, destinationPole);

// Move the n-1 disks from auxiliary (now source)
// pole to destination pole using source pole as
// temporary (auxiliary) pole
tower(n - 1, auxiliaryPole, destinationPole, sourcePole);
}

public static void main(String[] args)
{
tower(3, 'S', 'D', 'A');
}
}

// This code is contributed by Smitha Dinesh Semwal.``````

##### Python3
``````# Assuming n-th disk is
# bottom disk (count down)
def tower(n, sourcePole, destinationPole, auxiliaryPole):

# Base case (termination condition)
if(0 == n):
return

# Move first n-1 disks
# from source pole
# to auxiliary pole
# using destination as
# temporary pole
tower(n-1, sourcePole, auxiliaryPole, destinationPole)

# Move the remaining
# disk from source
# pole to destination pole
print("Move the disk",sourcePole,"from",sourcePole,"to",destinationPole)

# Move the n-1 disks from
# auxiliary (now source)
# pole to destination pole
# using source pole as
# temporary (auxiliary) pole
tower(n-1, auxiliaryPole, destinationPole,sourcePole)

# Driver code
tower(3, 'S', 'D', 'A')``````

##### C#
``````// Assuming n-th disk is bottom disk
// (count down)
using System;

class GFG {

static void tower(int n, char sourcePole,
char destinationPole,
char auxiliaryPole)
{

// Base case (termination condition)
if (0 == n)
return;

// Move first n-1 disks from source
// pole to auxiliary pole using
// destination as temporary pole
tower(n - 1, sourcePole, auxiliaryPole,
destinationPole);

// Move the remaining disk from source
// pole to destination pole
Console.WriteLine("Move the disk " + n
+ "from " + sourcePole + "to "
+ destinationPole);

// Move the n-1 disks from auxiliary
// (now source) pole to destination
// pole using source pole as temporary
// (auxiliary) pole
tower(n - 1, auxiliaryPole,
destinationPole, sourcePole);
}

// Driver code
public static void Main()
{
tower(3, 'S', 'D', 'A');
}
}

// This code is contributed by Anant Agarwal.``````

##### Javascript
``````

// Assuming n-th disk is bottom disk (count down)
function tower(n, sourcePole,
destinationPole, auxiliaryPole)
{

// Base case (termination condition)
if(0 == n)
return;

// Move first n-1 disks from source pole
// to auxiliary pole using destination as
// temporary pole
tower(n - 1, sourcePole, auxiliaryPole,
destinationPole);

// Move the remaining disk from source
// pole to destination pole
document.write("Move the disk " + n + " from " +
sourcePole + n + " to " + destinationPole + "<br>");

// Move the n-1 disks from auxiliary (now source)
// pole to destination pole using source pole as
// temporary (auxiliary) pole
tower(n - 1, auxiliaryPole, destinationPole,
sourcePole);
}

// Driver code
tower(3, 'S', 'D', 'A');

// This code is contributed by Manoj

``````

##### C++
``````void recursive(int data)
{
static callDepth;

if(callDepth > MAX_DEPTH)
return;

// Increase call depth
callDepth++;

// do other processing
recursive(data);

// do other processing
// Decrease call depth
callDepth--;
}

// This code is contributed by rutvik_56``````

##### C
``````void recursive(int data)
{
static callDepth;
if(callDepth > MAX_DEPTH)
return;

// Increase call depth
callDepth++;

// do other processing
recursive(data);

// do other processing
// Decrease call depth
callDepth--;
}``````

##### Java
``````static void recursive(int data)
{
static callDepth;
if(callDepth > MAX_DEPTH)
return;

// Increase call depth
callDepth++;

// do other processing
recursive(data);

// do other processing
// Decrease call depth
callDepth--;
}

// This code is contributed by divyeh072019``````

##### Python3
``````def recursive(data):

callDepth = 0

if(callDepth > MAX_DEPTH):
return;

# Increase call depth
callDepth+=1

# do other processing
recursive(data);

# do other processing
# Decrease call depth
callDepth -= 1

# This code is contributed by Pratham76``````

##### C#
``````static void recursive(int data)
{
static callDepth;
if(callDepth > MAX_DEPTH)
return;

// Increase call depth
callDepth++;

// do other processing
recursive(data);

// do other processing
// Decrease call depth
callDepth--;
}

// This code is contributed by divyeshrabadiya07``````

``````Move the disk 1 from S to D
Move the disk 2 from S to A
Move the disk 1 from D to A
Move the disk 3 from S to D
Move the disk 1 from A to S
Move the disk 2 from A to D
Move the disk 1 from S to D``````

TOH的时间复杂度可以通过制定移动次数来计算。

M N = 2M N-1 + 1。

## C++

``````void recursive(int data)
{
static callDepth;

if(callDepth > MAX_DEPTH)
return;

// Increase call depth
callDepth++;

// do other processing
recursive(data);

// do other processing
// Decrease call depth
callDepth--;
}

// This code is contributed by rutvik_56
``````

## C

``````void recursive(int data)
{
static callDepth;
if(callDepth > MAX_DEPTH)
return;

// Increase call depth
callDepth++;

// do other processing
recursive(data);

// do other processing
// Decrease call depth
callDepth--;
}
``````

## Java

``````static void recursive(int data)
{
static callDepth;
if(callDepth > MAX_DEPTH)
return;

// Increase call depth
callDepth++;

// do other processing
recursive(data);

// do other processing
// Decrease call depth
callDepth--;
}

// This code is contributed by divyeh072019
``````

## Python3

``````def recursive(data):

callDepth = 0

if(callDepth > MAX_DEPTH):
return;

# Increase call depth
callDepth+=1

# do other processing
recursive(data);

# do other processing
# Decrease call depth
callDepth -= 1

# This code is contributed by Pratham76
``````

## C＃

``````static void recursive(int data)
{
static callDepth;
if(callDepth > MAX_DEPTH)
return;

// Increase call depth
callDepth++;

// do other processing
recursive(data);

// do other processing
// Decrease call depth
callDepth--;
}

// This code is contributed by divyeshrabadiya07
``````

callDepth深度取决于函数堆栈帧大小和最大堆栈大小。