当前位置:网站首页>函数递归和动态内存初识
函数递归和动态内存初识
2022-08-02 14:02:00 【iccoke】
函数递归
递归函数是数论函数的一种,其定义域与值域都是自然数集,只是由于构作函数方法的不同而有别于其他的函数。处处有定义的函数叫做全函数,未必处处有定义的函数叫做部分函数。最简单又最基本的函数有三个:零函数O(x)=0(其值恒为0);射影函数;后继函数S(x)=x+1。它们合称初始函数。要想由旧函数作出新函数,必须使用各种算子。
那么函数递归就是在函数的参数包含问题规模,在函数的内部直接或者间接调用原函数,其核心思想就是分解与合并

例如我们实现一个n位数的和
int gets(int n) {
int count = 0;
for (int i = 1; i <= n; i++) {
count += i;
}
return count;
}
int main() {
int n=4;
int res = gets(n);
printf("%d", res);
}这一个简单的函数,那么如果我们用递归的思想处理这个问题应该如何思考呢
依据思想,先分解

那么这样就把分解成了这样,递归结束的条件是当n=1的时候结束

那么这就是他的一个代码的递归过程
再例如,用递归实现二分查找算法,获取一个整形的各个位数并输出
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
#include<assert.h>
#include<string.h>
int binarysearch(int* arr, int front, int end, int mid, int value) {
assert(arr != NULL && front <= mid);
while (front <= end) {
if (value == arr[mid]) {
return mid;
}
if (value < arr[mid]) {
end = mid - 1;
mid = (front + end) / 2;
return binarysearch(arr, front, end, mid, value);
}
else {
front = mid + 1;
mid = (front + end) / 2;
return binarysearch(arr, front, end, mid, value);
}
}
return -1;
}
int main() {
int arr[] = { 1,2,3,4,5,6,7,8 };
int value = 0;
int front = 0;
int end = sizeof(arr) / sizeof(arr[0]) - 1;
int mid = end / 2;
int res = binarysearch(arr, front, end, mid, value);
printf("%-5d", res);
return 0;
}#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
#include<assert.h>
#include<string.h>
void getnum(int n) {
int num = 0;
if (n != 0) {
printf("%5d", n % 10);
getnum(n / 10);
}
}
int main() {
int n = 123;
getnum(n);
}这些都是比较经典的递归问题
递归的特点和优点
特点
1. 每一级函数调用时都有自己的变量,但是函数代码并不会得到复制,如计算5的阶乘时每递推一次变量都不同;
2. 每次调用都会有一次返回,如计算5的阶乘时每递推一次都返回进行下一次;
3. 递归函数中,位于递归调用前的语句和各级被调用函数具有相同的执行顺序;
4. 递归函数中,位于递归调用后的语句的执行顺序和各个被调用函数的顺序相反;
5. 递归函数中必须有终止语句。
优点
就是可以代替循环,循环可以干的递归也可以,但是递归能干的循环不一定可以
动态内存
这里就涉及到了函数的调用机制

栈和堆

在使用堆进行内存申请的时候,一般会用到malloc函数,在使用malloc函数后,要用free释放掉申请的内存,并且将这块内存空间置空。
边栏推荐
- Creating seven NiuYun Flask project complete and let cloud
- Raj delivery notes - separation 第08 speak, speaking, reading and writing
- drf路由组件Routers
- YOLOv7使用云GPU训练自己的数据集
- [ROS] (06) ROS Communication - Topic Communication
- 瑞吉外卖笔记——第10讲Swagger
- redis delay queue
- 深度学习框架pytorch快速开发与实战chapter4
- [ROS](04)package.xml详解
- window10 lower semi-automatic labeling
猜你喜欢

Unit 14 Viewsets and Routing

redis分布式锁和看门狗的实现

What are the file encryption software?Keep your files safe

yolov5改进(一) 添加注意力集中机制
![[ROS] Introduction to common tools in ROS (to be continued)](/img/ea/e390106f750bf697e62a3a296014d2.png)
[ROS] Introduction to common tools in ROS (to be continued)

chapter7

8581 Linear linked list inversion
![[ROS] The difference between roscd and cd](/img/a8/a1347568170821e8f186091b93e52a.png)
[ROS] The difference between roscd and cd

Sentinel源码(四)(滑动窗口流量统计)

Flask项目的完整创建 七牛云与容联云
随机推荐
[ROS](03)CMakeLists.txt详解
The 2nd China Rust Developers Conference (RustChinaConf 2021~2022) Online Conference Officially Opens Registration
理解TCP长连接(Keepalive)
Swagger 的使用
深度学习框架pytorch快速开发与实战chapter3
IDEA打包jar包
Linux: CentOS 7 install MySQL5.7
yolov5改进(一) 添加注意力集中机制
Flask framework in-depth
Steps to connect the virtual machine with xshell_establish a network connection between the host and the vm virtual machine
MobileNet ShuffleNet & yolov5 replace backbone
Verilog Learning Series
Cloin 控制台乱码
8576 Basic operations of sequential linear tables
【ROS】工控机的软件包不编译
Briefly write about the use and experience of PPOCRLabel
Flask-SQLAlchemy
[ROS] Compiling packages packages encounters slow progress or stuck, use swap
Unit 8 Middleware
YOLOv7使用云GPU训练自己的数据集