// liner_time_max_subarray.cpp #include <iostream> #include <stdio.h> int my_sum(int const * p, int const n, int & begin, int & end) { int max; int sum; int max_all = p[0]; unsigned int b = 0; unsigned int e = 0; for (int i = 0; i < n; ++i) { sum = 0; max = p[i]; for (int j = i; j < n; ++j) { sum += p[j]; if (sum > max) { max = sum; b = i; e = j; } } if (max > max_all) { max_all = max; begin = b; end = e; } } return max_all; } int main() { int a[] = { 1, -2, 3, 10, -4, 7, 2, -5 }; int i = 0; int j = 0; std::cout << my_sum(a, sizeof(a)/ sizeof(int), i, j) << std::endl; for (int m = i; m < j; m++) { std::cout << a[m] << " "; } std::cout << std::endl; getchar(); return 0; }
#include<stdio.h> #include<stdlib.h> #include<limits.h> int maxsubset(int* a, int len) // 暴力violate { int summax = INT_MIN; int i, j, k; for(i = 0; i < len; i++) { for(j = i; j < len; j++) { int temp = 0; for(k = i; k <= j; k++) temp += a[k]; if(temp > summax) summax = temp; } } return summax; } int main() { int a[] = { 1, -10, 2, 4, 6, -15, 6, 1, 9, -8 }; printf("the maxsubset:%d ", maxsubset(a, sizeof(a) / sizeof(int))); getchar(); return 0; }