#include<iostream> using namespace std; int MaxSubSum(int *a, int num) { int curSum=0; int maxSum=a[0]; for(int i=0; i<num; i++) { for(int j=i; j<num; j++) { for(int k=i; k<=j; k++) { curSum+=a[k]; } if(curSum>maxSum) maxSum=curSum; curSum=0; } } return maxSum; } int main() { int a[8]={1, -2, 3, 10, -4, 7, 2, -5}; int num=8; int maxSum=MaxSubSum(a, num); cout<<maxSum<<endl; return 0; }
#include<iostream> using namespace std; int MaxSubSum(int *a, int num) { int curSum=0; int maxSum=a[0]; for(int i=0; i<num; i++) { curSum=(curSum+a[i]>a[i]) ? curSum+a[i] : a[i]; maxSum=maxSum>curSum ? maxSum : curSum; } return maxSum; } int main() { int a[8]={1, -2, 3, 10, -4, 7, 2, -5}; int num=8; int maxSum=MaxSubSum(a, num); cout<<maxSum<<endl; return 0; }
原文地址:http://blog.csdn.net/puqutogether/article/details/41800387