#include <iostream> using namespace std; int main() { int a[] = {1,-1,3,-7,10,-3,6,-8,1}; int size = sizeof(a)/sizeof(int); int curSum = 0; int maxSum = a[0]; for(int i=0;i<size;i++){ curSum = (curSum+a[i] > a[i]) ? curSum+a[i] : a[i]; if(curSum>maxSum){ maxSum = curSum; } } printf("maxSum:%d",maxSum); }
原文地址:http://blog.csdn.net/qinning199/article/details/43487239