标签:
最大子数组的和
double get_max_sum(double a[], int length) { double cursum = a[length - 1]; double maxsum = a[length - 1]; int curi = length - 2; while (curi >= 0) { cursum = max(a[curi], a[curi] + cursum); maxsum = max(cursum, maxsum); -- curi; } return maxsum; }
最大子数组的积
double get_max_pro(double a[], int length) { double minEnd = a[length - 1]; double maxEnd = a[length - 1]; double maxresult = a[length - 1]; int curi = length - 2; while (curi >= 0) { double end1 = a[curi] * maxEnd; double end2 = a[curi] * minEnd; minEnd = min(a[curi], min(end1, end2)); maxEnd = max(a[curi], max(end1, end2)); -- curi; } return maxEnd; }
测试
#include <iostream> using namespace std; int main() { double is[] = {-2, 5, 3, -6, 4, -8, 6}; double ds[] = {-2, 5, 3, -6, 4, -8, 6}; cout << get_max_sum(is, 7) << endl; cout << get_max_pro(ds, 7) << endl; return 0; }
标签:
原文地址:http://www.cnblogs.com/datakv/p/5632418.html