原题:http://tech-queries.blogspot.com/2009/05/max-possible-sum-of-non-consecutive.html
这里使用DP来解答
但是既然都不是连续的 而且是一个正数数组 那么只有2中情况 一种是奇数idx 一种是偶数idx
不过还是给出DP代码:
#include <cstdlib>
#include <algorithm>
int findMaxSum(int* a, int n)
{
int* s = (int*)malloc(sizeof(int) * n);
s[0] = a[0];
s[1] = a[1];
int maxSum = std::max(s[0], s[1]);
for(int i = 2; i < n; i++)
{
s[i] = std::max(s[i - 1], s[i - 2] + a[i]);
if(s[i] > maxSum)
maxSum = s[i];
}
return maxSum;
};
int main()
{
int a[] = {6, 4, 2, 8, 1};
int res = findMaxSum(a, 5);
return 0;
}