子序列的最大和
#include <stdio.h>
using namespace std;
int maxSubArraySum(int a[],int size)
{
int max_ending_here = 0;
int max_so_far = a[0];
for (int i=0; i<size; i++) {
max_ending_here = max_ending_here+a[i];
if (max_ending_here>max_so_far) {
max_so_far = max_ending_here;
}
if (max_ending_here<0) {
max_ending_here=0;
}
}
return max_so_far;
}
int main(int argc, const char * argv[]) {
int a[] = {-2, -3, 4, -1, -2, 1, 5, -3};
int size = sizeof(a)/sizeof(int);
int ans = maxSubArraySum(a, size);
printf("%d\n", ans);
return 0;
}
- 注意:数组传参问题,数组的大小一定要和数组一并传入函数。
- 不能在函数内部求传入的数组的大小