求最大子列和
2017-06-30 本文已影响0人
Alan66
int MaxSubSeqSum(int arr[], int n) {
int currentSum, maxSum;
currentSum = maxSum = 0;
for(int i=0; i<n; i++) {
currentSum += arr[i]; /* 向右累加 */
if(currentSum > maxSum) { /* 发现更大值 更新最大值 */
maxSum = currentSum;
}else if(currentSum < 0){ /* 如果当前子列为负 舍弃 置零 */
currentSum = 0;
}
}
return maxSum;
}