#include <iostream>
using namespace std;
const int MIN_INT = -2147483647;
int maxSum(const int *arr, int len){
int my_max = MIN_INT;
int tmp = 0;
for(int i = 0; i < len; i++){//从头到尾。。
tmp += arr[i];//遍历相加。
if(my_max < tmp){//更新my_max
my_max = tmp;
}
if(tmp < 0){//起了反作用。。
tmp = 0;//从头来
}
}
return my_max;
}
int main(){
int arr[8] = {1, -2, 3, 10, -4, 7, 2, -5};
cout<<"最大值:"<<maxSum(arr, 8) <<endl;
return 0;
}
求整型数组所有子串的和中的最大值
最新推荐文章于 2021-11-07 22:48:16 发布