1 int maxsequence3(int a[], int len) 2 { 3 int maxsum, maxhere; 4 maxsum = maxhere = a[0]; //初始化最大和为a【0】 5 for (int i=1; i<len; i++) { 6 if (maxhere <= 0) 7 maxhere = a[i]; //如果前面位置最大连续子序列和小于等于0,则以当前位置i结尾的最大连续子序列和为a[i] 8 else 9 maxhere += a[i]; //如果前面位置最大连续子序列和大于0,则以当前位置i结尾的最大连续子序列和为它们两者之和 10 if (maxhere > maxsum) { 11 maxsum = maxhere; //更新最大连续子序列和 12 } 13 } 14 return maxsum; 15 }