博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
求子数组的最大和
阅读量:4316 次
发布时间:2019-06-06

本文共 1134 字,大约阅读时间需要 3 分钟。

/** * 求子数组的最大和 */#include 
#include
bool func(const int inArr[], const size_t inLength, int &outResult){ if (inArr == NULL || inLength == 0) { return false; } int tmpSum = 0; outResult = 0; /// 只累加正和 for (size_t i = 0; i < inLength; i++) { tmpSum += inArr[i]; // 累加 tmpSum = tmpSum < 0 ? 0 : tmpSum; // 和小于0就清零 outResult = tmpSum > outResult ? tmpSum : outResult; // 判断最大值 } /// 如果所有元素都是负数 if (0 == outResult) { outResult = inArr[0]; for (size_t i = 1; i < inLength; i++) { outResult = inArr[i] > outResult ? inArr[i] : outResult; } } return true;}int main(){ const size_t SIZ = 10; const int arr[SIZE] = {-2, 4, 1, -5, 5, -1, 5, 5, 1, -10}; int largest = 0; if (func(arr, SIZE, largest)) { printf("最大和:%d\n", largest); } else { printf("查找失败\n"); }#ifdef _DEBUG system("pause");#endif; return EXIT_SUCCESS;}

 

posted on
2014-09-26 10:36 阅读(
...) 评论(
...)  

转载于:https://www.cnblogs.com/ciano/articles/3994343.html

你可能感兴趣的文章
获取各种类型的节点
查看>>
表达式求值-201308081712.txt
查看>>
centos中安装tomcat6
查看>>
从Vue.js窥探前端行业
查看>>
学习进度
查看>>
poj3368 RMQ
查看>>
“此人不存在”
查看>>
github.com加速节点
查看>>
解密zend-PHP凤凰源码程序
查看>>
python3 序列分片记录
查看>>
Atitit.git的存储结构and 追踪
查看>>
atitit 读书与获取知识资料的attilax的总结.docx
查看>>
B站 React教程笔记day2(3)React-Redux
查看>>
找了一个api管理工具
查看>>
Part 2 - Fundamentals(4-10)
查看>>
使用Postmark测试后端存储性能
查看>>
NSTextView 文字链接的定制化
查看>>
第五天站立会议内容
查看>>
CentOs7安装rabbitmq
查看>>
(转))iOS App上架AppStore 会遇到的坑
查看>>