🎈:经典之所以被称为经典,是因为在大部分的题目中都能够找到它们的影子,经典题的解题思路已潜移默化的渗透到每道题中,只有我们掌握好经典题的解题思路,我想我们解题能力也定会更上一层楼。
🎈:这篇博客主要是对一些经典的递归题目进行讲解,让你对递归题不再恐惧!!
![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/1233bc67611e44adb5f5591a466f027c.png)
题1:
题目描述
:sunny::sunny::sunny:递归方式实现打印一个整数的每一位
代码编写(C语言版本)
void print(int n) {
if (n > 9) {
print(n / 10);
}
printf("%d ", n % 10);
}
结果演示
![在这里插入图片描述 在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/3b1e7d8792e24bf49bd22739c1f0d388.png?x-oss-process=image/resize,w_1400/format,webp)
题2:
题目描述
:sunny::sunny::sunny: 递归求n的阶乘(不考虑溢出的问题)
代码编写(C语言版本)
int mul(int n) {
if (n <= 1) {
return 1;
}
return n * mul(n - 1);
}
结果演示
![在这里插入图片描述 在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/549991d7df80437888c9a9e6797729b4.png?x-oss-process=image/resize,w_1400/format,webp)
题3:
题目描述
:sunny::sunny::sunny: strlen的模拟(递归实现)
代码编写(C语言版本)
int my_strlen(char* arr) {
if (*arr == '\0')
return 0;
else return 1 + my_strlen(arr + 1);
}
结果演示
![在这里插入图片描述 在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/f90699ddfad840e5be6a1b91a492a4d9.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA54as5aSc56OV5Luj56CB5Li2,size_20,color_FFFFFF,t_70,g_se,x_16&x-oss-process=image/resize,w_1400/format,webp)
题4:
题目描述
:sunny::sunny::sunny:编写一个函数 reverse_string(char * string,int left,int right)(递归实现)
实现:将参数字符串中的字符反向排列,不是逆序打印。
要求:不能使用C函数库中的字符串操作函数。
代码编写(C语言版本)
void reverse_string(char* arr,int left,int right) {
if (left >= right) {
return arr;
}
char h = arr[left];
arr[left] = arr[right];
arr[right] = h;
reverse_string(arr, left + 1, right - 1);
}
结果演示
![在这里插入图片描述 在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/a32b48e3cf0a4ee5bbf32741fd8432b3.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA54as5aSc56OV5Luj56CB5Li2,size_20,color_FFFFFF,t_70,g_se,x_16&x-oss-process=image/resize,w_1400/format,webp)
题5:
题目描述
:sunny::sunny::sunny:写一个递归函数DigitSum(n),输入一个非负整数,返回组成它的数字之和
例如,调用DigitSum(1729),则应该返回1+7+2+9,它的和是19
输入:1729,输出:19
代码编写(C语言版本)
int DigitSum(int n) {
if (n > 9) {
return n % 10 + DigitSum(n / 10);
}
}
结果演示
![在这里插入图片描述 在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/01b33b930e304fcda46b1e6bff039872.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA54as5aSc56OV5Luj56CB5Li2,size_14,color_FFFFFF,t_70,g_se,x_16&x-oss-process=image/resize,w_1400/format,webp)
题6:
题目描述
:sunny::sunny::sunny:编写一个函数实现n的k次方,使用递归实现
代码编写(C语言版本)
int my_pow(n, k) {
if (k <= 1) {
return n;
}
return n * my_pow(n,k - 1);
}
结果演示
![在这里插入图片描述 在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/3845601fbccb4fdc96828a2f71014404.png?x-oss-process=image/resize,w_1400/format,webp)
题7:
题目描述
:sunny::sunny::sunny:递归和非递归分别实现求第n个斐波那契数
例如:
输入:5 输出:5
输入:10, 输出:55
输入:2, 输出:1
代码编写(C语言版本)
int dig(int n) {
if (n <=2) {
return 1;
}
return dig(n - 1) + dig(n - 2);
}
结果演示
![在这里插入图片描述 在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/f284e30d87e442ff978a93d6278e5157.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA54as5aSc56OV5Luj56CB5Li2,size_14,color_FFFFFF,t_70,g_se,x_16&x-oss-process=image/resize,w_1400/format,webp)