当前位置: 首页 > java >正文

C语言编程--递归程序--求数组的最大元素值

任务:Finding the maximum among N N N items stored in an array a [ 0 ] a[0] a[0], … \ldots , a [ N − 1 ] a[N - 1] a[N1].

实现:

#include <stdio.h>
typedef char Item;Item max(Item a[], int l, int r);Item max2(Item a[], int N);int main(int argc, char** argv){
Item a[] = {'T', 'I', 'N', 'Y', 'E', 'X', 'A', 'M', 'P', 'L', 'E'};Item maxNum = max2(a, 11);printf("%c\n", maxNum);return 0;
}/*** Program 5.6 Divide-and-conquer to find the maximum* -------------------------------------------------------------------------------------------------* This function divides a file a[l],. . . , a[r] into a[l],. . . , a[m] and a[m+1],. . . , a[r],* finds the maximum elements in the two parts (recursively), and * returns the larger of the two as the maximum element in the whole file. * It assumes that Item is a first-class type for which > is defined.* If the file size is even, the two parts are equal in size; * if the file size is odd, the size of the first part is 1 greater than the size of the second part.*/
Item max(Item a[], int l, int r){if (l == r) {return a[l];}Item u, v;int m = (l+r)/2;u = max(a, l, m);v = max(a, m+1, r);if (u > v) {return u;}else {return v;}}Item max2(Item a[], int N){// Item t = a[0];// int i;// for (i = 1; i < N; i++) {//     if (a[i] > t) {//         t = a[i];//     }// }// return t;return max(a, 0, N-1);
}

示例程序5.6的递归分析示意图
示例程序5.6的递归分析示意图

递归调用树结构表示
递归调用树

  • 后续遍历就是调用顺序加返回顺序

C语言编程环境支持示例递归计算的内部栈内容
示例程序在运行时的内部栈内容

http://www.xdnf.cn/news/4331.html

相关文章:

  • 油气地震资料信号处理中的NMO(正常时差校正)
  • 【网络篇】传输层TCP协议的确认应答,超时重传机制
  • IT咨询——企业数据资产怎样评估
  • 满分PPT | 基于数据运营的新型智慧城市实践与思考智慧城市数据中台解决方案智能建筑与智慧城市建设方案
  • 基于nacos实现动态线程池设计与实践:告别固定配置,拥抱弹性调度
  • LabVIEW与 IMAQ Vision 机器视觉应用
  • C++类与对象基础续
  • 15.命令模式:思考与解读
  • 毫米波雷达原理(最通俗的解释)
  • MATLAB程序演示与编程思路,相对导航,四个小车的形式,使用集中式扩展卡尔曼滤波(fullyCN-EKF)
  • go 编译报错:build constraints exclude all Go files
  • Python使用爬虫ip抓取热点新闻
  • autojspro怎么免费用
  • 【原创分享】魔音变声器内含超多语音包实时变声
  • C#中从本地(两个路径文件夹)中实时拿图显示到窗口中并接收(两个tcp发送的信号)转为字符串显示在窗体中实现检测可视化
  • 【Wandb】搜索框仅支持正则匹配,不接受“tags:“前缀查询
  • 知乎前端面试题及参考答案
  • 【计算机网络】TCP为什么可靠?解决了哪些问题?
  • 数字文明时代开源技术驱动的商业范式重构:基于开源AI大模型、AI智能名片与S2B2C商城小程序源码的协同创新研究
  • GD32F470+CH395Q
  • Odoo 开发:揭秘表单视图中“添加行”按钮的出现条件
  • Netty 是一个基于 Java NIO 的高性能网络通信框架
  • 基于SSM实现的健身房系统功能实现一
  • C++复习
  • Linux进程间通信(上)(21)
  • Unity学习笔记二
  • Java 8 非对称加密代码示例
  • cpp学习笔记2--class
  • STL详解 - map和set
  • tinyrenderer笔记(Shader)