给定一个数组序列,找出其中连续子序列和的最大值
思路:从头开始,只要当前序列和大于0,就对后续序列有正向作用,只要当前序列和大于和的最大值就更换最大值。
1 #include<stdio.h>
2 int maxSubSequenceSum(int *arr , int size){
3 int maxsum = -10000;
4 int sum = 0;
5 for(int i=0; i<size; i++){
6 if(sum<0)sum=arr[i];
7 else sum += arr[i];
8 if(sum>maxsum) maxsum=sum;
9 }
10 return maxsum;
11 }
12 int main(){
13 int arr1[]={-9,-2,-3,-5,-3};
14 int arr2[]={0,-2,3,5,-1};
15 int a=maxSubSequenceSum(arr1,5);
16 int b=maxSubSequenceSum(arr2,5);
17 printf("%d,%d\n",a,b);
18
19 }