Data Structures C Code 3.4.5: Recursion (Accumulation & Towers of Hanoi)

 Think and understand:

1. Disadvantages: Recursive implementation, in a sense, complicates simple problems, and has a higher time complexity than dynamic programming.

2. Advantages: It is of great significance for us to understand the role of the stack, time and space complexity, etc.

Learning objectives: understand and master the recursive algorithm, and combine recursion and stack to make Tower of Hanoi exercises.

Study Guide: Fanshen Code Accumulation  Tower of Hanoi

Learning tasks:

  1. copy code
  2. Learning Outcome Catalog

    1 Data Structure C Code 3.4: Recursive Implementation of Accumulation

    1.1 All codes

    1.2 Test results

    2 Data Structure C Code 3.5: The Tower of Hanoi Problem

    1.1 All codes

    1.2 Test results

    1.3 Drawings and understanding

Code description:

        1 cumulative

        1.1 This is the simplest recursive program, not one of them.
        1.2 The system will automatically build a stack for the recursive program, and the stack stores functions and corresponding data (local variables, etc.).
        1.3 The time complexity of this accumulation is different from that of the for loop Consistent, both are O(n)O(n)O(n), but the space complexity is O(n)

        1.4O(n)O(n), higher than the O ( 1 ) O(1)O(1) of the for loop. The stack space used. 1.5clearAddTo is a clear version, without debugging statements
        .

        2 Tower of Hanoi

        2.1 The recursive implementation of accumulation is purely for laying the foundation for this program.
        2.2 The Tower of Hanoi problem is very simple, and the function is only 9 lines when it is fully loaded
        ; From here we can see the advantages of using meaningful and long variable names: the readability of the program is greatly improved.
        2.4 The initial condition is 0 plates, so it is more convenient to judge the negative number of plates.
        2.5 The time complexity of the algorithm is O(2n)O(2^n)O(2n), the space complexity is O(n)O(n)O(n). All need to draw pictures to explain.
 

study-time:

2022.5.12

1 Data Structure C Code 3.4: Recursive Implementation of Accumulation

1.1 All codes

#include <stdio.h>
/**
 * @brief 递归加法。
 * 
 * @param paraN 
 * 
 * @return 和
 */
int addTo(int paraN) {
	int tempSum;
	printf("进入addTo(%d)\n", paraN);
	if (paraN <= 0) {
		printf(" return 0\n");
		return 0;
	} else {
		tempSum = addTo(paraN - 1) + paraN;
		printf(" return %d\n", tempSum);
		return tempSum;
	}
}
/**
 * 递归累加测试类
 */
void addToTest() {
	int n, sum;
	printf("---- addToTest开始测试 ----\n");
	
	n = 5;
	sum = addTo(n);
	printf("0 +...+ %d = %d.\n\n", n, sum);
	
	n = 1;
	sum = addTo(n);
	printf("0 +...+ %d = %d.\n\n", n, sum);
	
	n = -1;
	sum = addTo(n);
	printf("0 +...+ %d = %d.\n\n", n, sum);
	
	
	printf("---- addToTest结束测试 ----\n");
}
int main() {
	addToTest();
	return 0;
}

1.2 Test results

---- addToTest开始测试 ----
进入addTo(5)
进入addTo(4)
进入addTo(3)
进入addTo(2)
进入addTo(1)
进入addTo(0)
 return 0
 return 1
 return 3
 return 6
 return 10
 return 15
0 +...+ 5 = 15.

进入addTo(1)
进入addTo(0)
 return 0
 return 1
0 +...+ 1 = 1.

进入addTo(-1)
 return 0
0 +...+ -1 = 0.

---- addToTest结束测试 ----

--------------------------------
Process exited after 0.03249 seconds with return value 0

Press ANY key to continue...

2 Data Structure C Code 3.5: The Tower of Hanoi Problem

1.1 All codes

#include <stdio.h>
/**
 * @brief 汉诺塔移动递归函数
 * 
 * @param paraDestination 
 * @param paraN 
 * @param paraSource 
 * @param paraTransit 
 */
int cnt=0;
void hanoi(int paraN, char paraSource, char paraDestination, char paraTransit) {
	
	if (paraN <= 0) {
		return;
	} else {
		cnt++;
		hanoi(paraN - 1, paraSource, paraTransit, paraDestination);
		printf(" %c -> %c \n", paraSource, paraDestination);
		hanoi(paraN - 1, paraTransit, paraDestination, paraSource);
	}
}
/**
 * @brief 测试类
 */
void hanoiTest() {
	printf("---- addToTest 开始 ----\n");
	int N;
	printf("请输入加入的盘子数:");
	scanf("%d",&N);
	
	printf("%d个盘子的移动过程:\n",N);
	hanoi(N, 'A', 'B', 'C');
	
	printf("共移动%d次\n\n",cnt);
	printf("---- addToTest 结束 ----");
}
int main(){
	hanoiTest();
	return 0;
}

1.2 Test results

move two

---- addToTest 开始 ----
请输入加入的盘子数:2
2个盘子的移动过程:
 A -> C
 A -> B
 C -> B
共移动3次

---- addToTest 结束 ----

move three

---- addToTest 开始 ----
请输入加入的盘子数:3
3个盘子的移动过程:
 A -> B
 A -> C
 B -> C
 A -> B
 C -> A
 C -> B
 A -> B
共移动7次

---- addToTest 结束 ----

1.3 Drawings and understanding

  • n = 1 hour,
    •  Move the plate directly from A to C;
  • n > 1 hour,
    •  First move the above n - 1 plates from A to B (subproblem, recursive);
    •  Then move the largest plate from A to C;
    •  Then move n - 1 plates on B from B to C (subproblem, recursive).

  

  

Supongo que te gusta

Origin blog.csdn.net/qq_61649579/article/details/124739086
Recomendado
Clasificación