一、代码块
累加递归详情
int addTo(int paraN){
int tempSum;
printf("entering addTo(%d)\r\n", paraN);
//递归结尾
if(paraN <= 0){
printf(" return 0\r\n");
return 0;
}
//开启下一个相加函数
else{
tempSum = addTo(paraN - 1) + paraN;
printf(" return %d\r\n", tempSum);
return tempSum;
}
}
累加递归
int clearAddTo(int paraN){
if(paraN <= 0){
return 0;
}
else{
return clearAddTo(paraN - 1) + paraN;
}
}
hanoi的实现
void hanoi(int paraN, char paraSource, char paraDestination, char paraTransit){
if (paraN <= 0){
return;
}
else{
//将paraN-1个盘子,借助目的区,转移到过渡区
hanoi(paraN - 1, paraSource, paraTransit, paraDestination);
//将大盘子转移到目的区
printf("%c -> %c \r\n", paraSource, paraDestination);
//将paraN-1个盘子,借助起始区,转移到目的区
hanoi(paraN - 1, paraTransit, paraDestination, paraSource);
}
}
二、全部代码
//
// main.c
//
// Created by ABC on 2022/5/11.
//
#include <stdio.h>
//累加递归详情
int addTo(int paraN){
int tempSum;
printf("entering addTo(%d)\r\n", paraN);
//递归结尾
if(paraN <= 0){
printf(" return 0\r\n");
return 0;
}
//开启下一个相加函数
else{
tempSum = addTo(paraN - 1) + paraN;
printf(" return %d\r\n", tempSum);
return tempSum;
}
}
//累加递归
int clearAddTo(int paraN){
if(paraN <= 0){
return 0;
}
else{
return clearAddTo(paraN - 1) + paraN;
}
}
//相加递归测试
void addToTest(){
int n, sum;
printf("---- addToTest begins. ----\r\n");
n = 5;
sum = addTo(n);
printf("\r\n0 adds to %d gets %d.\r\n", n, sum);
n = 5;
sum = clearAddTo(n);
printf("\r\n0 adds to %d gets %d.\r\n", n, sum);
n = 1;
sum = addTo(n);
printf("\r\n0 adds to %d gets %d.\r\n", n, sum);
n = 1;
sum = clearAddTo(n);
printf("\r\n0 adds to %d gets %d.\r\n", n, sum);
n = -1;
sum = addTo(n);
printf("\r\n0 adds to %d gets %d.\r\n", n, sum);
n = -1;
sum = clearAddTo(n);
printf("\r\n0 adds to %d gets %d.\r\n", n, sum);
printf("---- addToTest ends. ----\r\n");
}
//hanoi的实现
void hanoi(int paraN, char paraSource, char paraDestination, char paraTransit){
if (paraN <= 0){
return;
}
else{
//将paraN-1个盘子,借助目的区,转移到过渡区
hanoi(paraN - 1, paraSource, paraTransit, paraDestination);
//将大盘子转移到目的区
printf("%c -> %c \r\n", paraSource, paraDestination);
//将paraN-1个盘子,借助起始区,转移到目的区
hanoi(paraN - 1, paraTransit, paraDestination, paraSource);
}
}
//hanoi测试
void hanoiTest(){
printf("---- addToTest begins. ----\r\n");
printf("2 plates\r\n");
hanoi(2, 'A', 'B', 'C');
printf("3 plates\r\n");
hanoi(3, 'A', 'B', 'C');
printf("4 plates\r\n");
hanoi(4, 'A', 'B', 'C');
printf("5 plates\r\n");
hanoi(5, 'A', 'B', 'C');
printf("---- addToTest ends. ----\r\n");
}
//main
void main(){
addToTest();
hanoiTest();
}
三、运行结果
---- addToTest begins. ----
entering addTo(5)
entering addTo(4)
entering addTo(3)
entering addTo(2)
entering addTo(1)
entering addTo(0)
return 0
return 1
return 3
return 6
return 10
return 15
0 adds to 5 gets 15.
0 adds to 5 gets 15.
entering addTo(1)
entering addTo(0)
return 0
return 1
0 adds to 1 gets 1.
0 adds to 1 gets 1.
entering addTo(-1)
return 0
0 adds to -1 gets 0.
0 adds to -1 gets 0.
---- addToTest ends. ----
---- addToTest begins. ----
2 plates
A -> C
A -> B
C -> B
3 plates
A -> B
A -> C
B -> C
A -> B
C -> A
C -> B
A -> B
4 plates
A -> C
A -> B
C -> B
A -> C
B -> A
B -> C
A -> C
A -> B
C -> B
C -> A
B -> A
C -> B
A -> C
A -> B
C -> B
5 plates
A -> B
A -> C
B -> C
A -> B
C -> A
C -> B
A -> B
A -> C
B -> C
B -> A
C -> A
B -> C
A -> B
A -> C
B -> C
A -> B
C -> A
C -> B
A -> B
C -> A
B -> C
B -> A
C -> A
C -> B
A -> B
A -> C
B -> C
A -> B
C -> A
C -> B
A -> B
---- addToTest ends. ----
四、小结
递归时间复杂度2^n,空间复杂度n
执行次数为2^n-1,时间复杂度为2^n
栈空间重复使用,空间复杂度为n
递归算法耗费时间较久,占用空间较多,平时应减少递归使用。